在向程序添加框架时遇到一些困难

时间:2018-08-17 22:20:16

标签: python tkinter

我正在尝试更改程序的布局,但在添加框架以及将小部件添加到这些框架中时遇到了困难。部分原因是我还不完全了解下面编写的代码。我用*表示从框架的角度我需要帮助理解的部分。

我遇到的问题是我想将屏幕分成半宽度,以便窗口的上半部分显示为红色,而底部显示为蓝色。为此,我将一侧分别包装为顶部和底部。但是,它们之间有空白。我该如何删除呢?

谢谢

class The99club (Tk):

    def __init__ (self, *args, **kwargs):

        Tk.__init__(self,*args,**kwargs)
        Tk.title(self, 'The 99 Club')

        container = Frame(self)

        container.grid(row = 0, column = 0, sticky='nsew')*
        container.grid_rowconfigure (2,weight = 1)*
        container.grid_columnconfigure (2,weight = 1)*

        self.frames = {}

        for F in (StartPage,PageOne,PageTwo,PageThree,PageFour,PageFive):

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

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller
        self.top_frame = Frame(self, bg = 'red')
        self.top_frame.pack(side = TOP, fill= X)
        self.bottom_frame = Frame (self, bg = 'blue')
        self.bottom_frame.pack(side = BOTTOM, fill = X)

        label = Label(self.top_frame, text = 'Welcome to The 99 Club', bg = '#66B132',fg='yellow', font = ('Agent Red',30))
        label.pack()

        label2 = Label(self.top_frame, text = 'Press Enter to begin.',bg = '#66B132',fg='yellow',font = ('Comic Sans MS',24))
        label2.pack()
        self.button = Button(self, text = 'Enter',bg = 'yellow',command=lambda: controller.show_frame(PageOne),font = ('Comic Sans MS', 24),relief = SUNKEN)
        self.button.pack()

1 个答案:

答案 0 :(得分:0)

基本上,您可能没有意识到,但是空白实际上是框架的边界。要删除它,必须在帧的参数中设置borderwidth = 0和highlightthickness = 0。因此,它看起来像:

self.top_frame = Frame(self, bg = 'red', borderwidth=0, highlightthickness=0)

让我知道这是否行不通。