gpt4 book ai didi

python - 如果我们最大化窗口,则调整小部件的大小

转载 作者:行者123 更新时间:2023-12-04 15:18:18 25 4
gpt4 key购买 nike

如果窗口最大化,我想展开小部件。请找到下面的示例代码,如果我们运行它,我们将得到一些 TreeView ,但如果我们最大化窗口,那么 TreeView 将移动到左上角,是否有可能使该 View 在最大化后展开并完美适合。谢谢

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()

最佳答案

首先添加:

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)

关于python - 如果我们最大化窗口,则调整小部件的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63917305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com