更改tkinter中由类定义的框架大小

4

我想要改变中的框架大小;我正在使用Python 3.0,并为学校项目创建库存管理类型的东西。这是我现在拥有的代码,我正在尝试编辑框架的尺寸。我已经试图在网上寻找所需信息,但没有找到。我知道你需要使用:

.geomtry()

内置函数,但我不知道该把它放在哪里,或者如何与类一起使用。
                import tkinter as tk

            LARGE_FONT= ("Comic Sans MS", 12)
            SMALL_FONT=('Comic sans ms', 8)

            class StockManager(tk.Tk):

                def __init__(self, *args, **kwargs):
                    tk.Tk.__init__(self, *args, **kwargs)
                    container = tk.Frame(self)

                    container.pack(side="top", fill="both", expand=True)

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

                    self.frames = {}

                    for F in (StartPage, PageOne):
                        frame = F(container, self)

                        self.frames[F] = frame

                        frame.grid(row=0, column=0, sticky="nsew")


                    self.show_frame(StartPage)

                def show_frame(self, cont):
                    frame = self.frames[cont]
                    frame.tkraise()


            class StartPage(tk.Frame):

                def __init__(self, parent, controller):
                    tk.Frame.__init__(self, parent)

                    lbl1 = tk.Label(self, text='Welcome to the stock manager', font=LARGE_FONT)
                    lbl1.pack(pady=10, padx=10)
                    lbl2 = tk.Label(self, text='What would you like to do?', font=LARGE_FONT)
                    lbl2.pack(pady=10, padx=10)

                    btnShowProducts = tk.Button(self, text='1. Show all products ', font=LARGE_FONT,)
                    btnShowProducts.pack(anchor='w')

                    btnSearchProducts = tk.Button(self, text='2. Search for a product ', font=LARGE_FONT)
                    btnSearchProducts.pack(anchor='w')

                    btnAddProduct = tk.Button(self, text='3. Add another product ', font=LARGE_FONT)
                    btnAddProduct.pack(anchor='w')

                    btnDeleteProduct = tk.Button(self, text='4. Delete a product ', font=LARGE_FONT)

                    btnClose = tk.Button(self, text='Close Database ', font=LARGE_FONT)
                    btnClose.pack(anchor='w')

                    btnNextPage = tk.Button(self, text='Next Page', font=SMALL_FONT)
                    btnNextPage.pack(anchor='s')


            class PageOne(tk.Frame):

                def __init__(self, parent, controller):
                    tk.Frame.__init__(self, parent)
                    label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)
                    label.pack(pady=10, padx=10)

                    btnAddTrans = tk.Button(self, text='1. Add a new transaction', font=('comic sans ms', 10))  ##CREATE ADD TRANSACTION HERE

                    btnShowTrans = tk.Button(self, text='2. Show Transactions', font=('Comic Sans MS', 10))
                    btnShowTrans.pack(anchor='w')

                    button1 = tk.Button(self, text="Back to Home")
                    button1.pack()

                    button2 = tk.Button(self, text="Next Page")
                    button2.pack()

            menu = StockManager()
            menu.mainloop()

请提供一个可行的示例。目前,如果我复制粘贴您的代码,它将无法运行。 - Mike - SMT
@Mike-SMT,我现在会编辑问题并附上一些你可以运行的代码。抱歉。 - bobdaves69
@Mike-SMT编辑了兄弟。 - bobdaves69
@bobdaves69 谢谢你的编辑,但我们不需要整个程序。只需将其保留为一个按钮,其中包含您已尝试过的内容以及您卡住的地方即可。这是最小、完整和可验证示例的一部分。话虽如此,我已经提供了一个示例,说明如何使用geometry()wininfo更改窗口大小。 - Mike - SMT
@ReblochonMasque 我的意思是Python 3。 - bobdaves69
显示剩余3条评论
2个回答

4
这里是一个简单的示例,展示如何使用 geometry() 方法来调整窗口大小。这将有助于您更好地理解它在类中的工作方式。
import tkinter as tk


class My_App(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        # we need to set parent as a class attribute for later use
        self.parent = parent 
        button1 = tk.Button(self.parent, text="Make window larger!", command = self.make_window_bigger)
        button1.pack()

        button2 = tk.Button(self.parent, text="Make window Smaller!", command = self.make_window_smaller)
        button2.pack()

    def make_window_bigger(self):
        x = self.parent.winfo_height() + 10
        y = self.parent.winfo_width() + 10
        self.parent.geometry('{}x{}'.format(y, x))

    def make_window_smaller(self):
        x = self.parent.winfo_height() - 10
        y = self.parent.winfo_width() - 10
        self.parent.geometry('{}x{}'.format(y, x))

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

如果我要在类中设置尺寸,一种方法是简单地执行以下操作: self.parent=parent self.parent.geometry(x,y) - bobdaves69
不太对。你需要将其格式化为字符串,因为几何图形接受字符串格式。你需要像这样做:('{}x{}'.format(y, x)。话虽如此,其他部分是正确的。你只需要将父类定义为类属性并像我展示的那样使用几何图形即可。 - Mike - SMT
@bobdaves69 没问题。在你测试的时候告诉我它的表现如何。 - Mike - SMT

2
如果您想调整ToplevelTk实例的大小,可以使用对象上的geometry方法:
root.geometry("{}x{}+{}+{}".format(16, 32, 64, 128))
#self.geometry("{}x{}+{}+{}".format(16, 32, 64, 128))
#self.winfo_toplevel().geometry()("{}x{}+{}+{}".format(16, 32, 64, 128))

如果您想调整 Frame 的大小,只需设置其 widthheight 选项即可:

frame = tk.Frame(root, bg='red', width=32, height=23)
#self.config(bg='red', width=32, height=23)

如果框架非空,您还应该取消其传播,以防止基于其子小部件大小需求进行调整大小,这取决于子级使用的几何管理器:

frame.pack_propagate(False) # discard either in accordance with the children
frame.grid_propagate(False)
#self.pack_propagate(False) # discard either in accordance with the children
#self.grid_propagate(False)

2
你的回答有点不太清楚。如果框架非空,如果你想在框架本身上指定宽度和高度,那么将传播设置为False是不可选的。我认为你知道这一点,但你的回答没有表达得很清楚。 - Bryan Oakley
@BryanOakley 谢谢你的帮助,我现在觉得更清楚了。最初,我认为如果内部小部件被销毁,它会调整到给定的大小,但这似乎是错误的。应该在小部件达到所需大小后立即取消传播。 - Nae

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接