【发布时间】:2021-08-20 14:44:07
【问题描述】:
我希望当 GUI 窗口的大小根据显示器大小增加或减少时,我的 gui 小部件得到管理。
使用我当前的代码,这个窗口在我的显示器中看起来像这样 --- Look of the gui when open in my pc
当在另一个相同大小的监视器屏幕中打开相同的 GUI 程序时,窗口的某些部分会被截断 -- Look of the gui window in another pc monitor
但我希望我的 GUI 窗口能够通过监视器屏幕调整其所有小部件。
请帮我解决这个问题,并请建议我应该在我的代码中添加什么以使其响应!!
注意 - 我使用了 pack() 方法来放置标签框
我的某行代码---
class Application:
def __init__(self, root):
self.root = root
self.root.title("Auto-Garage Management Application")
self.screen_width = self.root.winfo_screenwidth()
self.screen_height = self.root.winfo_screenheight()
self.root.geometry("%dx%d+-10+0" % (self.screen_width, self.screen_height))
bg_color1 = "#ff704d"
bg_color2 = "powder blue"
# =============== Fonts
frame_label_font = Font(family="Poppins", size=20, weight="bold")
entry_label_font1 = Font(family="Roboto", size=17, weight="bold")
entry_label_font2 = Font(family="Roboto", size=15, weight="bold")
entry_font = Font(family="Noto Sans", size=15, weight="bold")
btn_font = Font(family="Noto Sans", size=15, weight="bold")
# ================ Variables
# Customer Details
self.customer_name = StringVar()
self.customer_phone = StringVar()
self.customer_telno = StringVar()
self.customer_address_1 = StringVar()
self.customer_address_2 = StringVar()
self.customer_address_3 = StringVar()
# ========== Customer Details
customer_details_frame = LabelFrame(self.root, text="Customer Details", bg=bg_color1, fg="black",
font=frame_label_font, bd=6, relief=GROOVE)
customer_details_frame.pack(fill=BOTH)
# Customer Name
customer_name = Label(customer_details_frame, text="Customer Name", bg=bg_color1, fg="yellow", font=entry_label_font1)
customer_name.grid(row=0, column=0, padx=20, pady=5)
customer_name_txt = Entry(customer_details_frame, textvariable=self.customer_name, width=22, font=entry_font, bd=5, relief=SUNKEN)
customer_name_txt.grid(row=0, column=1, padx=10)
# Customer Phone Number
customer_phone = Label(customer_details_frame, text="Customer Phone", bg=bg_color1, fg="yellow", font=entry_label_font1)
customer_phone.grid(row=0, column=2, padx=20, pady=5)
customer_phone_txt = Entry(customer_details_frame, textvariable=self.customer_phone, width=22, font=entry_font, bd=5, relief=SUNKEN)
customer_phone_txt.grid(row=0, column=3, padx=10)
customer_phone2 = Label(customer_details_frame, text="Customer Tel No.", bg=bg_color1, fg="yellow", font=entry_label_font1)
customer_phone2.grid(row=1, column=2, padx=10)
customer_phone2_txt = Entry(customer_details_frame, textvariable=self.customer_telno, width=22, font=entry_font, bd=5, relief=SUNKEN)
customer_phone2_txt.grid(row=1, column=3, padx=10)
# Customer Address
customer_address = Label(customer_details_frame, text="Customer Address", bg=bg_color1, fg="yellow", font=entry_label_font1)
customer_address.grid(row=0, column=4, padx=20, pady=5)
customer_address_txt1 = Entry(customer_details_frame, textvariable=self.customer_address_1, width=22, font=entry_font, bd=5, relief=SUNKEN)
customer_address_txt1.grid(row=0, column=5, padx=10)
customer_address_txt2 = Entry(customer_details_frame, textvariable=self.customer_address_2, width=22, font=entry_font, bd=5, relief=SUNKEN)
customer_address_txt2.grid(row=1, column=5, padx=10)
customer_address_txt3 = Entry(customer_details_frame, textvariable=self.customer_address_3, width=22, font=entry_font, bd=5, relief=SUNKEN)
customer_address_txt3.grid(row=2, column=5, padx=10, pady=5)
app = Tk()
software = Application(app)
app.mainloop()
感谢您的帮助!!!
【问题讨论】:
-
向我们展示一些代码会有所帮助。
-
我认为使用 tkinter 创建响应式布局并不容易。
-
我还能用什么来在 python 中而不是 tkinter 中制作响应式 gui??
-
@acw1668:这取决于您如何定义“响应式”。制作适合多种窗口大小的 UI 非常容易。然而,让字体自动放大或缩小需要更多的工作。不过还是可以的。
-
看起来您的问题主要是由于使用了对于较小的显示器来说太大的硬编码字体。简单的答案似乎是“使用较小的字体”。如果没有看到我们可以用来查看您如何创建 GUI 的最小示例,我们将很难为您提供更好的建议。