在Tkinter中调整小部件以在任何大小的屏幕上工作



我是一名业余程序员,很久以前我使用Gtk,最近使用PyQt来创建gui,但刚刚回到Tkinter,因为它的优雅。在Gtk和Qt中,网格命令似乎以自动方式调整窗口大小的小部件。在Tkinter我真的很困惑如何设置一个窗口是可调整大小。使用Brian Oakley的建议:我如何让我的tkinter应用适合任何尺寸的屏幕?和各种来源引用我试着写以下简单的代码,所以它应该在任何尺寸的屏幕上工作。由于某些原因,我无法更改父列的大小,也无法将LB列表框扩展到屏幕底部。如果我添加宽度= 10,高度= 75的LB定义线,它确实扩展,但当然LB的底部将丢失当窗口调整大小。我意识到这是非常混乱的代码,但我认为它解释了问题。有人能告诉我该如何处理代码(尤其是LB),使其在各种屏幕尺寸上可用吗?提前谢谢你。

class Application(tk.Frame): # /5104330/
#
def __init__(self, parent, *args, **kwargs): # /17466561/
tk.Frame.__init__(self, parent, *args, **kwargs) # /17466561/
self.parent = parent # root - /17466561/
# Makes column 1 three times wider than column 2
# THIS DOES NOT HAVE ANY EFFECT. WHY NOT?:
self.parent.columnconfigure(0, weight=3)
self.parent.columnconfigure(1, weight=1)
# Setup frame
frame1 = tk.Frame(self)# /61989498/
frame1.grid(row=0, column=0, rowspan = 20, columnspan = 8, sticky='nsew')
# Create widgets
scrollbar = tk.Scrollbar(frame1, orient=tk.VERTICAL) # /10870855/, /32715745/
self.LB = tk.Listbox(frame1, yscrollcommand=scrollbar.set)#, width = 10, height = 75)
scrollbar.config(command=self.LB.yview)
# Pack the widgets
self.LB.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y, expand=1)
# insert data
for r in range(0, 50):
self.LB.insert(tk.END, str(r)+'-LB1')
# As per https://stackoverflow.com/questions/31885234/, /53073534/ and /64545856/
for i in range(0,35):
# None of these extend the self.LB down to the bottom of the window:
self.parent.grid_rowconfigure(i, weight=1)
frame1.grid_rowconfigure(i, weight=1)
self.LB.grid_rowconfigure(i, weight=1)
#
self.parent.rowconfigure(i, weight=1)
frame1.rowconfigure(i, weight=1)
self.LB.rowconfigure(i, weight=1)
#
frame2 = tk.Frame(self, bd=1, relief='flat', bg='white')# , width = 12, height = 700) # /61989498/
frame2.grid(row=1, column=9, sticky='nsew', rowspan=40, columnspan = 3, ipadx=4)
self.slbl = tk.Label(frame2, text ='Enter Search Term:'); self.slbl.pack(side=tk.TOP, padx=20)
if __name__ == "__main__":  # /17466561/
root = tk.Tk()
Application(root).pack(side='top', fill='both', expand=True)
root.attributes('-zoomed', True) # maximize the window
root.mainloop()

虽然我不喜欢回答自己的问题,但我认为解决方案只是删除pack命令并在根窗口上使用grid来解决问题。简化我使用:

import tkinter as tk
class Application(tk.Frame):
#
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
# Create widgets
scrollbar = tk.Scrollbar(self.parent, orient=tk.VERTICAL)
self.LB = tk.Listbox(self.parent, yscrollcommand=scrollbar.set)
scrollbar.config(command=self.LB.yview)
# Pack the widgets
self.LB.grid(row=0, column=0, rowspan = 98, columnspan = 8, sticky='nsew')
scrollbar.grid(row=0, column=8, rowspan = 98, columnspan = 1, sticky='nsw')
# insert data
for r in range(0, 50):
self.LB.insert(tk.END, str(r)+'-LB1')
for i in range(0,98):
# Extend the self.LB down to the bottom of the maximized window
self.parent.grid_rowconfigure(i, weight=1)
self.slbl = tk.Label(self.parent, text ='Enter Search Term:')
self.slbl.grid(row=1, column=9, sticky='nsew', rowspan=40, columnspan = 3, ipadx=4)
if __name__ == "__main__":
root = tk.Tk()
Application(root)
root.attributes('-zoomed', True) # maximize the window
root.mainloop()
##########################################################

最新更新