无法在tkinter上使用网格调整树视图大小

问题描述 投票:1回答:1

我无法使用网格布局调整tkinter上的treeview小部件。我尝试使用columnconfigurerowconfigureweight,但它不起作用。当我运行它时,当我拉伸它所在的窗口时,树视图不会填充空白区域。

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")
python python-3.x tkinter resize treeview
1个回答
3
投票

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

之后 ...

第二:你必须调整Frameself)的大小,所以它将使用完整的窗口

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

因为TreeviewFrame里面然后你必须使用columnconfigureFrame

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

工作实例。我评论command=来运行它。

(顺便说一句:我在bg="red"中使用Frame来查看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()
© www.soinside.com 2019 - 2024. All rights reserved.