简体   繁体   English

Tkinter、treeview 不会随 window 调整大小

[英]Tkinter, treeview doesn't resize with window

How can I make Treeview resize and keep the Sizegrip in the se corner of the window when I resize the window vertically and horizontally?当我垂直和水平调整 window 的大小时,如何使 Treeview 调整大小并使 Sizegrip 保持在 window 的 se 角? I have used frames, set sticky="ns" to resize Frame in window and fill='BOTH', expand=True to resize Treeview in Frame.我使用了框架,设置 sticky="ns" 以在 window 中调整 Frame 和 fill='BOTH', expand=True 以在 Frame 中调整 Treeview 的大小。

from tkinter import *           # Python interface to the Tk GUI toolkit
from tkinter import filedialog  # open file
from tkinter import ttk         # Tk themed widget set

root = Tk()

#menu itmes removed for space

win2 = Toplevel(master=root)    # create a new top level window
frame1 = Frame(win2)
frame2 = Frame(win2)
frame3 = Frame(win2)
scrollbar = Scrollbar(frame1)     # put a scrolbar widget on the right side of the window
scrollbar.pack(side = RIGHT, fill = Y)
sizegrip=ttk.Sizegrip(frame3)     # put a sizegrip widget on the southeast corner of the window
sizegrip.pack(side = RIGHT, anchor = SE)

# put a treeview widget on the window with stylized column headers and use show="headings" to hide the first hierarchy column
column_headers=['PID', 'Name', 'DNA Company Name', 'DNA User Name', 'Admin Name', 'Email']
style = ttk.Style()
style.configure("Treeview.Heading", font=("Verdana", 11))
tv = ttk.Treeview(frame1, height=30, columns=column_headers, show="headings", yscrollcommand = scrollbar.set) 
tv.pack(side=LEFT, fill=BOTH, expand=TRUE)
scrollbar.config(command = tv.yview)

export_button = ttk.Button(frame2, text = "Export", width=15,command=win2.destroy)
export_button.pack(side = LEFT, anchor = E, padx=5, pady=5)

close_button = ttk.Button(frame2, text = "Close", width=15, command=win2.destroy)
close_button.pack(side = RIGHT, anchor = W, padx=5, pady=5)

tv.heading('PID', text='PID')
tv.column('PID', anchor='w', width = 80)

tv.heading('Name', text='Name')
tv.column('Name', anchor='w')

tv.heading('DNA Company Name', text='DNA Company Name')
tv.column('DNA Company Name', anchor='w')

tv.heading('DNA User Name', text='DNA User Name')
tv.column('DNA User Name', anchor='w')

tv.heading('Admin Name', text='Admin Name')
tv.column('Admin Name', anchor='w')

tv.heading('Email', text='Email')
tv.column('Email', anchor='w')

frame1.grid(column=0, row=0, sticky="ns")
frame2.grid(column=0, row=1, sticky="n")
frame3.grid(column=0, row=2, sticky="se")
root.rowconfigure(0, weight=1)

root.mainloop()

Use the following in the respective places where you are putting the widgets on the screen:在屏幕上放置小部件的相应位置使用以下内容:

packing the Frame that will hold the TreeView:包装将容纳 TreeView 的框架:

frame1.pack(padx=19, fill='both', expand="yes") frame1.pack(padx=19, fill='both', expand="yes")

packing the TreeView ont tv.pack(padx=19, fill='both', expand="yes")打包 TreeView ont tv.pack(padx=19, fill='both', expand="yes")

It is because you are not setting up your frame1 for resizing.这是因为您没有设置frame1来调整大小。 First, you need to setup the Toplevel for resizing row 0 and column 0:首先,您需要设置Toplevel以调整第 0 行和第 0 列的大小:

win2.grid_rowconfigure(0, weight=1)
win2.grid_columnconfigure(0, weight=1)

Finally, you need to change the frame's sticky to "nsew":最后,您需要将框架的粘性更改为“nsew”:

frame1.grid(column=0, row=0, sticky="nsew")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM