gpt4 book ai didi

python - 无法在 tkinter 上使用网格调整 TreeView 大小

转载 作者:行者123 更新时间:2023-11-30 22:23:24 25 4
gpt4 key购买 nike

我无法使用网格布局调整 tkinter 上的 TreeView 小部件的大小。我尝试将 columnconfigurerowconfigureweight 一起使用,但它不起作用。当我运行它时,当我拉伸(stretch) TreeView 所在的窗口时, TreeView 不会填充空白空间。

def __init__(self, master):
self.master = master
tk.Frame.__init__(self, self.master)
self.create_widgets()

def create_widgets(self):
# button to request data
self.request_button = tk.Button(root, text="Send", command=self.req_client)
self.request_button.grid(row=0, column=0, rowspan=2, columnspan=3)

# timer label
self.timer_label = tk.Label(root, text="Timer Settings")
self.timer_label.grid(row=0, column=3, columnspan=2, pady=3, sticky=tk.NSEW)

# create tk variable
self.timervar = tk.StringVar(root)

# dropdown dictionary
self.timerDict = {"-", "5 minutes", "10 minutes", "15 minutes"}
self.timervar.set("-") # <-- set the default value

# timer dropdown menu
self.timer_option = tk.OptionMenu(root, self.timervar, *self.timerDict, command=self.req_timer)
self.timer_option.grid(row=1, column=3, columnspan=2, padx=3, pady=3, sticky=tk.NSEW)

# scroll bar for the terminal outputs
self.terminal_scrollbar = tk.Scrollbar(root)
self.terminal_scrollbar.grid(row=2, column=5, sticky=tk.NS)

# terminal output
self.terminal_tree = ttk.Treeview(root)
self.terminal_tree.grid(row=2, column=0, columnspan=5, sticky=tk.NSEW)
self.terminal_tree.configure(yscrollcommand=self.terminal_scrollbar.set)
self.terminal_tree.columnconfigure(0, weight=1)
self.terminal_tree.rowconfigure(0, weight=1)
self.terminal_tree["columns"] = ("1")
self.terminal_tree['show'] = 'headings'
self.terminal_tree.column("1", width=100, anchor='c')
self.terminal_tree.heading("1", text="Example")

最佳答案

首先:如果您使用 Frame 创建类,那么您应该使用此框架 (self) 作为小部件的父级(而不是使用 root >)

之后...

第二:您必须调整Frame(self)的大小,以便它将使用完整窗口

self.pack(fill='both', expand=True)

因为Treeview位于Frame内部,所以你必须将columnconfigureFrame一起使用

self.columnconfigure(0, weight=1) # column with treeview
self.rowconfigure(2, weight=1) # row with treeview

工作示例。我注释了 command= 来运行它。

(顺便说一句:我在 Frame 中使用了 bg="red" 来查看 Frame 是否正在调整大小)

import tkinter as tk
from tkinter import ttk

class Main(tk.Frame):

def __init__(self, master):
self.master = master
tk.Frame.__init__(self, self.master) # , bg="red")

self.pack(fill='both', expand=True)

self.create_widgets()

def create_widgets(self):
# button to request data
self.request_button = tk.Button(self, text="Send") #, command=self.req_client)
self.request_button.grid(row=0, column=0, rowspan=2, columnspan=3)

# timer label
self.timer_label = tk.Label(self, text="Timer Settings")
self.timer_label.grid(row=0, column=3, columnspan=2, pady=3, sticky=tk.NSEW)

# create tk variable
self.timervar = tk.StringVar(self)

# dropdown dictionary
self.timerDict = {"-", "5 minutes", "10 minutes", "15 minutes"}
self.timervar.set("-") # <-- set the default value

# timer dropdown menu
self.timer_option = tk.OptionMenu(self, self.timervar, *self.timerDict) #, command=self.req_timer)
self.timer_option.grid(row=1, column=3, columnspan=2, padx=3, pady=3, sticky=tk.NSEW)

# scroll bar for the terminal outputs
self.terminal_scrollbar = tk.Scrollbar(self)
self.terminal_scrollbar.grid(row=2, column=5, sticky=tk.NS)

# terminal output
self.terminal_tree = ttk.Treeview(self)
self.terminal_tree.grid(row=2, column=0, columnspan=5, sticky=tk.NSEW)
self.terminal_tree.configure(yscrollcommand=self.terminal_scrollbar.set)
self.terminal_tree["columns"] = ("1")
self.terminal_tree['show'] = 'headings'
self.terminal_tree.column("1", width=100, anchor='c')
self.terminal_tree.heading("1", text="Example")

self.columnconfigure(0, weight=1) # column with treeview
self.rowconfigure(2, weight=1) # row with treeview

root = tk.Tk()
Main(root)
root.mainloop()

您可能必须在第一行内使用框架才能更好地组织按钮和选项菜单。

enter image description here

<小时/>

编辑:更改某些grid()中的选项并更改rowconfigure()

enter image description here

import tkinter as tk
from tkinter import ttk

class Main(tk.Frame):

def __init__(self, master):
self.master = master
tk.Frame.__init__(self, self.master) # , bg="red")

self.pack(fill='both', expand=True)

self.create_widgets()

def create_widgets(self):
# button to request data
self.request_button = tk.Button(self, text="Send") #, command=self.req_client)
self.request_button.grid(row=0, column=0, rowspan=2)

# timer label
self.timer_label = tk.Label(self, text="Timer Settings")
self.timer_label.grid(row=0, column=1, pady=3, sticky=tk.NSEW)

# create tk variable
self.timervar = tk.StringVar(self)

# dropdown dictionary
self.timerDict = {"-", "5 minutes", "10 minutes", "15 minutes"}
self.timervar.set("-") # <-- set the default value

# timer dropdown menu
self.timer_option = tk.OptionMenu(self, self.timervar, *self.timerDict) #, command=self.req_timer)
self.timer_option.grid(row=1, column=1, padx=3, pady=3, sticky=tk.NSEW)

# scroll bar for the terminal outputs
self.terminal_scrollbar = tk.Scrollbar(self)
self.terminal_scrollbar.grid(row=2, column=5, sticky=tk.NS)

# terminal output
self.terminal_tree = ttk.Treeview(self)
self.terminal_tree.grid(row=2, column=0, columnspan=3, sticky=tk.NSEW)
self.terminal_tree.configure(yscrollcommand=self.terminal_scrollbar.set)
self.terminal_tree["columns"] = ("1")
self.terminal_tree['show'] = 'headings'
self.terminal_tree.column("1", width=100, anchor='c')
self.terminal_tree.heading("1", text="Example")

self.columnconfigure(2, weight=1) # column with treeview
self.rowconfigure(2, weight=1) # row with treeview

root = tk.Tk()
Main(root)
root.mainloop()

关于python - 无法在 tkinter 上使用网格调整 TreeView 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48057591/

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