TKinter网格和继承的对象

时间:2017-12-31 22:36:54

标签: python tkinter

我正在使用here

中的代码

我正在尝试将一个简单的表插入到TKinter中的Frame中。我已经调整了代码,但我似乎无法使新对象尊重网格大小。

表格应该是全宽并且按钮应该显示,但是表格占据了窗口的一半,并且它的高度似乎是固定的,因为如果我更改按钮显示的主框架几何高度。

任何人都可以告诉我我做错了什么或提出了更好的选择,所以我不必在明年开始就把头撞在墙上:)

from tkinter import *
from tkinter import ttk
import tkinter.font as tkFont

class frmListMembers(Frame):

    def __init__(self, master):

        Frame.__init__(self, master)
        #self.grid()

        self.master = master
        self.master.title("List Members")
        self.master.geometry("500x240+0+0")        
        self.master.resizable(False, False)        
        self.focus_set()

        container = Frame(master)
        container.grid(row=0, column=0, padx=10,pady=10)
        container.grid_columnconfigure(0, weight=1)
        self.tblMembers = MemberListBox(container) #.grid(row=0,column=0)

        self.OkButton = Button(master, text="DONE", command=self.oKButtonClick).grid(row=2, column=0, pady=(5), padx=10, sticky="E")
        container.grid_rowconfigure(2, weight=1)



    def oKButtonClick(self):

        self.master.destroy()


class MemberListBox(object):
    """use a ttk.TreeView as a multicolumn ListBox"""
    def __init__(self, container):
        self.container = container
        self.tree = None
        self._setup_widgets()
        self._build_tree()


    def _setup_widgets(self):

        #container = Frame()
        #container.pack(fill='both', expand=True)

        # create a treeview with vertical scrollbar
        self.tree = ttk.Treeview(columns=car_header, show="headings")
        vsb = Scrollbar(orient="vertical", command=self.tree.yview)
        self.tree.configure(yscrollcommand=vsb.set, xscrollcommand=None)
        self.tree.grid(column=0, row=0, sticky='NSEW', in_=self.container)
        vsb.grid(column=1, row=0, sticky='NS', in_=self.container)

        #self.container.grid_columnconfigure(0, weight=1)
        #self.container.grid_rowconfigure(0, weight=1)

    def _build_tree(self):
        for col in car_header:
            self.tree.heading(col, text=col.title(),
            command=lambda c=col: sortby(self.tree, c, 0))
            # adjust the column's width to the header string
            self.tree.column(col,width=tkFont.Font().measure(col.title()))

        for item in car_list:
            self.tree.insert('', 'end', values=item)
            # adjust column's width if necessary to fit each value
            for ix, val in enumerate(item):
                col_w = tkFont.Font().measure(val)
                if self.tree.column(car_header[ix],width=None)<col_w:
                    self.tree.column(car_header[ix], width=col_w)


# the test data ...
car_header = ['car', 'repair', 'price', 'Mileage']
car_list = [
('Hyundai', 'brakes', 10999, 20101) ,
('Honda', 'light', 10999, 20101) ,
]
root = Tk()
frmListMembers(root)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

首先,我尽力保留您的代码,尽力创建a Minimal, Complete, and Verifiable example OP的代码:

from tkinter import *
from tkinter import ttk
import tkinter.font as tkFont

class frmListMembers(Frame):

    def __init__(self, master):

        Frame.__init__(self, master)

        self.master = master
        self.master.geometry("500x240+0+0")        
        self.master.resizable(False, False)

        container = Frame(master)
        container.grid(row=0, column=0, padx=10,pady=10, sticky='ew')
        self.tblMembers = MemberListBox(container) #.grid(row=0,column=0)

        Button(master, text="DONE").grid(row=2, column=0, pady=(5), padx=10, sticky="E")


class MemberListBox(object):
    """use a ttk.TreeView as a multicolumn ListBox"""
    def __init__(self, container):
        self.container = container
        self.tree = None
        self._setup_widgets()
        self._build_tree()


    def _setup_widgets(self):

        self.tree = ttk.Treeview(columns=car_header, show="headings")
        vsb = Scrollbar(orient="vertical", command=self.tree.yview)
        self.tree.configure(yscrollcommand=vsb.set, xscrollcommand=None)
        self.tree.grid(column=0, row=0, sticky='NSEW', in_=self.container)
        vsb.grid(column=1, row=0, sticky='NS', in_=self.container)

    def _build_tree(self):
        for col in car_header:
            self.tree.heading(col, text=col.title())
            # adjust the column's width to the header string
            self.tree.column(col,width=tkFont.Font().measure(col.title()))

        for item in car_list:
            self.tree.insert('', 'end', values=item)
            # adjust column's width if necessary to fit each value
            for ix, val in enumerate(item):
                col_w = tkFont.Font().measure(val)
                if self.tree.column(car_header[ix],width=None)<col_w:
                    self.tree.column(car_header[ix], width=col_w)


# the test data ...
car_header = ['car', 'repair', 'price', 'Mileage']
car_list = [
('Hyundai', 'brakes', 10999, 20101) ,
('Honda', 'light', 10999, 20101) ,
]
root = Tk()
frmListMembers(root)
root.mainloop()

使用上面的代码我已经替换为:

container.grid(row=0, column=0, padx=10,pady=10, sticky='ew')

到下面一行:

class MemberListBox(object):

使用:

container.grid(row=0, column=0, padx=10,pady=10, sticky='nsew')   # expand
self.tblMembers = MemberListBox(container)
container.grid_rowconfigure(0, weight=1)        # allow tree to resize height
container.grid_columnconfigure(0, weight=1)     # allow tree to resize width
#container.grid_columnconfigure(1, weight=1)    # don't allow scrollbar to rsz
self.master.grid_columnconfigure(0, weight=1)   # allow container to resize
self.master.grid_rowconfigure(0, weight=1)      # both its width and height

self.OkButton = Button(container, text="DONE")  # put button in container
self.OkButton.grid(row=2, column=0, pady=(5), padx=10, sticky="E")

导致:

from tkinter import *
from tkinter import ttk
import tkinter.font as tkFont

class frmListMembers(Frame):

    def __init__(self, master):

        Frame.__init__(self, master)

        self.master = master
        self.master.geometry("500x240+0+0")        
        self.master.resizable(False, False)

        container = Frame(master)
        container.grid(row=0, column=0, padx=10,pady=10, sticky='nsew')   # expand
        self.tblMembers = MemberListBox(container)
        container.grid_rowconfigure(0, weight=1)        # allow tree to resize height
        container.grid_columnconfigure(0, weight=1)     # allow tree to resize width
        #container.grid_columnconfigure(1, weight=1)    # don't allow scrollbar to rsz
        self.master.grid_columnconfigure(0, weight=1)   # allow container to resize
        self.master.grid_rowconfigure(0, weight=1)      # both its width and height

        self.OkButton = Button(container, text="DONE")  # put button in container
        self.OkButton.grid(row=2, column=0, pady=(5), padx=10, sticky="E")


class MemberListBox(object):
    """use a ttk.TreeView as a multicolumn ListBox"""
    def __init__(self, container):
        self.container = container
        self.tree = None
        self._setup_widgets()
        self._build_tree()


    def _setup_widgets(self):

        self.tree = ttk.Treeview(columns=car_header, show="headings")
        vsb = Scrollbar(orient="vertical", command=self.tree.yview)
        self.tree.configure(yscrollcommand=vsb.set, xscrollcommand=None)
        self.tree.grid(column=0, row=0, sticky='NSEW', in_=self.container)
        vsb.grid(column=1, row=0, sticky='NS', in_=self.container)

    def _build_tree(self):
        for col in car_header:
            self.tree.heading(col, text=col.title())
            # adjust the column's width to the header string
            self.tree.column(col,width=tkFont.Font().measure(col.title()))

        for item in car_list:
            self.tree.insert('', 'end', values=item)
            # adjust column's width if necessary to fit each value
            for ix, val in enumerate(item):
                col_w = tkFont.Font().measure(val)
                if self.tree.column(car_header[ix],width=None)<col_w:
                    self.tree.column(car_header[ix], width=col_w)


# the test data ...
car_header = ['car', 'repair', 'price', 'Mileage']
car_list = [
('Hyundai', 'brakes', 10999, 20101) ,
('Honda', 'light', 10999, 20101) ,
]
root = Tk()
frmListMembers(root)
root.mainloop()

注意:这个&#39;答案中没有一个部分。如果OP提供了一个更简单的例子,那就不会那么容易了。