python - Tkinter:如何构建适用于多种不同屏幕分辨率的应用程序

标签 python python-3.x tkinter

所以我正在使用 Tkinter 作为 GUI 框架使用 Python 开发一个应用程序。第一张是我同事电脑的截图,第二张是我电脑的截图。如何使小部件(按钮、标签、条目等)适应任何屏幕分辨率? GUI from Coworkers computer

GUI from My Computer

我的代码示例

self.e_fname = Entry(self)
    self.e_fname.place(relx=0.21, rely=0.18, height=40, relwidth=0.27)
    self.l_fname = Label(self, text='''First Name:''', font=font9,bg='#0066AB')
    self.l_fname.place(relx=0.055, rely=0.13, height=40, width=514)
    # last name
    self.e_lname = Entry(self)
    self.e_lname.place(relx=0.55, rely=0.18, height=40, relwidth=0.27)
    self.l_lname = Label(self, text='''Last Name:''', font=font9,bg='#0066AB')
    self.l_lname.place(relx=0.395, rely=0.13, height=40, width=514)
    # email
    self.e_mail = Entry(self)
    self.e_mail.place(relx=0.21, rely=0.29,height=40, relwidth=0.57)
    self.l_mail = Label(self, text='''Email Address:''', font=font9, bg='#0066AB')
    self.l_mail.place(relx=0.06, rely=0.24, height=31, width=514)
    # phone number
    self.e_phone = Entry(self)
    self.e_phone.place(relx=0.21, rely=0.40, height=40, relwidth=0.57)
    self.l_phone = Label(self, text= '''Phone Number:''', font=font9, bg='#0066AB')
    self.l_phone.place(relx=0.06, rely=0.36, height=31, width=514)
    # Address
    self.e_saddress = Entry(self)
    self.e_saddress.place(relx=0.21, rely=0.51, height=40, relwidth=0.57)
    self.adress = Label(self, text='''Address:''', font=font9, bg='#0066AB')
    self.adress.place(relx=0.05, rely=0.46, height=31, width=514)
    # Address Line 2
    self.e_saddress2 = Entry(self)
    self.e_saddress2.place(relx=0.21, rely=0.62, height=40, relwidth=0.57)
    self.adress2 = Label(self, text='''Address Confirmation:''', font=font9, bg='#0066AB')
    self.adress2.place(relx=0.075, rely=0.57, height=31, width=514)
    # State
    self.state = Label(self, text='''State:''', font=font9, bg='#0066AB')

最佳答案

让 UI 在不同分辨率和不同字体大小的系统上保留其布局的最佳方法是使用 gridpack 而不是 place. place 实际上只适用于非常特定类型的问题,而响应能力并不是它的强项。

在您的具体情况下,使用网格似乎是个好主意,因为您的小部件似乎按行和列布局。使用grid时,请注意利用所有选项,尤其是sticky属性。另外,请确保利用 rowconfigurecolumnconfigure 方法为部分或全部行和列赋予非零权重,以便将额外的空间分配给他们。

关于python - Tkinter:如何构建适用于多种不同屏幕分辨率的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51698528/

相关文章:

python - 为什么我的一个变量不需要声明而另一个变量需要声明?

python - 修复混合编码的 csv 中的编码错误

python - Tkinter 骰子滚筒

python - Tkinter 使用菜单栏命令无意递归...原因?

python - 如何模拟类的函数的返回值?

python - 计算字符串中的常见字符 Python

Python 3.7 及以上版本 : how to determine Linux distribution?

python - Python 中 Tkinter Canvas 上 .jpg 图像的滚动条

python - 如何使用虚构日期解决 "ValueError: day is out of range for month"问题?

python - Python 3 中的递归