如果窗口最大化,我想扩展小部件。请找到下面的示例代码,如果运行此代码,我们将获得一些树视图,但是如果我们最大化窗口,则树视图将移至左上角,是否有可能使该视图在最大化后扩展并完全适合。谢谢
import os
import tkinter as tk
import tkinter.ttk as ttk
import datetime
import re
import threading
class Window:
i=0
def __init__(self,master):
self.master=master
self.master.geometry('700x350+350+100')
self.label = tk.Label(self.master, text='Sample', font=("Arial", 20)).grid(row=0,columnspan=3)
cols = ('A','B','C')
self.treeview = ttk.Treeview(self.master, columns=cols)
v_scrollbar = ttk.Scrollbar(self.master, orient='vertical', command=self.treeview.yview)
self.treeview.config( yscrollcommand=v_scrollbar.set)
for col in cols:
self.treeview.heading(col, text=col)
self.treeview.column(col,minwidth=0,width=170)
self.treeview.grid(row=1, column=0)
v_scrollbar.grid(row=1, column=1, sticky='nes')
#--- main---
def main():
root = tk.Tk()
Window(root)
root.mainloop()
if __name__ == '__main__':
main()
答案 0 :(得分:2)
首先添加:
self.master.rowconfigure(1, weight=1)
self.master.columnconfigure(0, weight=1)
告诉布局管理器扩展放置treeview
的单元格以填充可用空间。
然后将sticky='nsew'
添加到self.treeview.grid(...)
:
self.treeview.grid(row=1, column=0, sticky='nsew')
下面是修改后的__init__()
:
def __init__(self,master):
self.master=master
self.master.geometry('700x350+350+100')
tk.Label(self.master, text='Sample', font=("Arial", 20)).grid(row=0,columnspan=3)
cols = ('A','B','C')
self.treeview = ttk.Treeview(self.master, columns=cols)
v_scrollbar = ttk.Scrollbar(self.master, orient='vertical', command=self.treeview.yview)
self.treeview.config( yscrollcommand=v_scrollbar.set)
for col in cols:
self.treeview.heading(col, text=col)
self.treeview.column(col,minwidth=0,width=170)
self.treeview.grid(row=1, column=0, sticky='nsew') # added sticky='nsew'
v_scrollbar.grid(row=1, column=1, sticky='ns')
# tell layout manager to expand the cell where the treeview is
self.master.rowconfigure(1, weight=1)
self.master.columnconfigure(0, weight=1)