Tkinter:如何为几种不同的屏幕分辨率构建应用程序

时间:2018-08-05 21:55:19

标签: 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')

1 个答案:

答案 0 :(得分:2)

使UI在具有不同分辨率和字体大小的系统上保留其布局的最佳方法是使用gridpack而不是placeplace确实仅对非常特定类型的问题有用,而响应能力不是它的强项。

在您的特定情况下,使用grid似乎是一个好主意,因为您的窗口小部件似乎是按行和列排列的。使用grid时,请注意利用所有选项,尤其是sticky属性。另外,请确保利用rowconfigurecolumnconfigure方法为部分或全部行和列赋予非零的权重,以便为它们分配额外的空间。