设置框架宽度和高度

时间:2017-12-15 09:54:25

标签: python-3.x tkinter

下面的代码是使用Python中的tkinter创建的。它呈现一个帧,在该帧上提供到下一帧的链接,在下一帧上,它提供到前一帧的链接。我没有得到如何设置框架的宽度和高度。请帮忙。

import tkinter as tk
LARGE_FONT = ("Verdana", 12)
class ImgComp(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, PageTwo):

            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()

def qf(param):
    print(param)

class StartPage(tk.Frame):

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

        label = tk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)
        button1 = tk.Button(self, text="Visit Page 1",
                           command=lambda: controller.show_frame(PageOne))
        button1.pack()

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)
        button1 = tk.Button(self, text="Back to Home",
                            command=lambda:controller.show_frame(StartPage))
        button1.pack()   

app = ImgComp()
app.mainloop()

1 个答案:

答案 0 :(得分:1)

更改container你设置widthheight的尺寸 你必须使用grid_propagate(False)来保留它。

    container = tk.Frame(self, width=500, height=250)
    #container['width'] = 500
    #container['height'] = 250
    container.grid_propagate(False)

enter image description here

如果您向所有Pages添加bacground,那么您会看到他们使用完整尺寸

frame = F(container, self)
frame['bg'] = 'red'

enter image description here

现在你可以组织内部元素来使用所有区域。

例如,您可以使用pack(side='bottom')

将按钮放在底部
   button1 = tk.Button(self, text="Visit Page 1", ...)
    button1.pack(side='bottom')

enter image description here

相关问题