使用tkin排除机架扩展故障

2024-10-03 00:20:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是让我的新生的自定义GUI应用程序(它是tkinter.Frame的子类)在调整父窗口的大小时独立地向两个方向扩展。当我第一次在一个只使用本机tkinter类的脚本中构建它时,这种方法是有效的(请参阅文章底部的代码),但自从我将它重新构造为一个子类(请参阅下面的第一段代码)以来,它就坚持在调整大小时保持其原始比例。也就是说,如果我最大化父窗口,主GUI框架只会按对角线方向展开,直到它到达屏幕底部。然后右边窗口的其余部分作为空白灰色空间留在左边。你知道吗

我的问题是:我在类实现中犯了什么错误导致了这个问题?你知道吗

Here is a screenshot的问题,下面是用于在图片中生成GUI的代码(请原谅难看的配色方案,它只是为了帮助我在设计/调试时保持框架的笔直):

[编辑以删除不必要的代码(在这一部分中,生成我看到的帧比最初想象的要多,所以我不能剪那么多):]

from tkinter import *
from tkinter import ttk

class CharDbGui(Frame):
    # Mage character database object
    def __init__(self, root, database=None, *args, **kwargs):
        Frame.__init__(self, root, *args, **kwargs)
        self.root = root

        ### Broad layout into color-coded frames first ###
        self.rowweight(range(1, 5), [10, 40, 40, 10])
        self.colweight(range(1, 4), [25, 75, 25])
        top_toolbar = Frame(self, bg="green", height=50, width=1)
        top_toolbar.grid(row=1, column=1, columnspan=4, sticky=E+W+N+S)
        leftbox = Frame(self, height=1, width=1)
        leftbox.grid(row=2, column=1, rowspan=2, sticky=N+S+E+W)
        midbox = Frame(self, bg="white", height=1, width=1)
        midbox.grid(row=2, column=2, rowspan=2, sticky=N+S+E+W)
        righttopbox = Frame(self, bg="black", height=1, width=1)
        righttopbox.grid(row=2, column=3, sticky=N+S+E+W)
        rightbottombox = Frame(self, bg="purple", height=1, width=1)
        rightbottombox.grid(row=3, column=3, sticky=N+S+E+W)
        bottombox = Frame(self, bg="orange", height=1, width=1)
        bottombox.grid(row=4, column=1, columnspan=4, sticky=N+S+E+W)
        entity_type_list = ttk.Treeview(leftbox)
        entity_type_list.pack(expand=True, side=LEFT, fill=BOTH)
        sb_etl = Scrollbar(leftbox)
        sb_etl.pack(side=LEFT, fill=Y)

        ### Placeholder widgets ###
        mainbox = Text(midbox, bg="white")
        mainbox.pack(expand=True, fill=BOTH)
        reserved = Text(rightbottombox, bg="purple", width=1)
        reserved.pack(expand=True, fill=BOTH)
        logbox = Text(bottombox, height=6, width=80, bg="orange")
        logbox.pack(expand=True, fill=BOTH)

        ### Create buttons at upper half of right edge ###
        # Create frame to contain the buttons
        bwidth = 12  # button width
        b1 = Button(righttopbox, text="New", width=12)
        b1.grid(row=1, column=1, sticky="")
        b2 = Button(righttopbox, text="Edit", width=12)
        b2.grid(row=2, column=1, sticky="")
        b3 = Button(righttopbox, text="Organize", width=12)
        b3.grid(row=3, column=1, sticky="")
        b4 = Button(righttopbox, text="Templates", width=12)
        b4.grid(row=4, column=1, sticky="")
        b5 = Button(righttopbox, text="Delete", width=12)
        b5.grid(row=5, column=1, sticky="")

        # Space buttons vertically
        righttopbox.grid_rowconfigure(1, weight=1)
        righttopbox.grid_rowconfigure(2, weight=1)
        righttopbox.grid_rowconfigure(3, weight=1)
        righttopbox.grid_rowconfigure(4, weight=1)
        righttopbox.grid_rowconfigure(5, weight=1)

        # add self to root
        self.pack(expand=True, fill=BOTH)

    def rowweight(self, index, weight):
        # Adjust weight(s) of row(s).
        if type(index) == int:  # Singleton case
            assert(type(weight) == int)
            self.root.grid_rowconfigure(index, weight=weight)
        else:  # Range case
            assert(len(index) == len(weight))
            for i, w in zip(index, weight):
                self.root.grid_rowconfigure(i, weight=w)

    def colweight(self, index, weight):
        # Adjust weight(s) of column(s).
        if type(index) == int:  # Singleton case
            assert(type(weight) == int)
            self.root.grid_columnconfigure(index, weight=weight)
        else:  # Range case
            assert(len(index) == len(weight))
            for i, w in zip(index, weight):
                self.root.grid_columnconfigure(i, weight=w)

    def fillDefaults(self):
        # Placeholder values to test with
        self.root.title("Test Database")
        # Populate the list with some initial values
        initvals_etl = ["Characters", "Items"]
        for x in initvals_etl:
            self.tree.insert('', END, x, text=str(x))

        y = self.tree.insert('', END, "nums", text="Numbers")
        for x in range(100):
            self.tree.insert(y, END, x, text=str(x))

    def launch(self):
        self.root.mainloop()


if __name__ == '__main__':
    root = Tk()
    gui = CharDbGui(root)
    root.wm_state('zoomed')
    gui.launch()

然后here is a picture我希望它看起来像什么,下面是用来生成它的非类代码:

[编辑以删除不必要的代码:]

from tkinter import *
from tkinter import ttk

### FUNCTION DEFINITIONS ###
def rowweight(index, weight):
    # Adjust weight(s) of row(s).
    if type(index) == int:  # Singleton case
        assert(type(weight) == int)
        master.grid_rowconfigure(index, weight=weight)
    else:  # Range case
        assert(len(index) == len(weight))
        for i, w in zip(index, weight):
            master.grid_rowconfigure(i, weight=w)


def colweight(index, weight):
    # Adjust weight(s) of column(s).
    if type(index) == int:  # Singleton case
        assert(type(weight) == int)
        master.grid_columnconfigure(index, weight=weight)
    else:  # Range case
        assert(len(index) == len(weight))
        for i, w in zip(index, weight):
            master.grid_columnconfigure(i, weight=w)


### BUILDING THE GUI LAYOUT AND WIDGETS ###
# Create window that frames all our pieces
master = Tk()
master.title("Test Database")

### Broad layout into frames first ###
rowweight(range(1, 5), [10, 40, 40, 10])
colweight(range(1, 4), [25, 75, 25])
top_toolbar = Frame(master, bg="green", height=50, width=1)
top_toolbar.grid(row=1, column=1, columnspan=4, sticky=E+W+N+S)
leftbox = Frame(master, height=1, width=1)
leftbox.grid(row=2, column=1, rowspan=2, sticky=N+S+E+W)
midbox = Frame(master, bg="white", height=1, width=1)
midbox.grid(row=2, column=2, rowspan=2, sticky=N+S+E+W)
righttopbox = Frame(master, bg="black", height=1, width=1)
righttopbox.grid(row=2, column=3, sticky=N+S+E+W)
rightbottombox = Frame(master, bg="purple", height=1, width=1)
rightbottombox.grid(row=3, column=3, sticky=N+S+E+W)
bottombox = Frame(master, bg="orange", height=1, width=1)
bottombox.grid(row=4, column=1, columnspan=4, sticky=N+S+E+W)

master.mainloop()

Tags: selfmasterindextypecolumnrootwidthframe
1条回答
网友
1楼 · 发布于 2024-10-03 00:20:02

小说在评论中对这个问题给出了正确的答案:

You set the weight for the root, but you need to set it for the Frame. Change self.root.grid_columnconfigure(index, weight=weight) to self.grid_columnconfigure(index, weight=weight), and the same for rows.

在设计这个课程时,我在回去给东西加上“根”的时候太过火了,直到在评论中被指出,我才注意到我这么做了。你知道吗

相关问题 更多 >