Python2.7Tkinter网格布局管理器工作不正常

2024-06-16 09:58:35 发布

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

尊敬的StackOverflow社区:

我正试着用Python编程,而我在网格布局管理器方面有点困难。我一直试图自己找到答案,并尝试了各种选择,但我就是无法让我的用户界面看起来像我想要的那样。在

我希望你能帮助我。不幸的是,我不能张贴照片,因为我是新来的。但基本上,我希望左边所有的彩色按钮,在第一列中,彼此间隔均匀。然后是第2列中的标签和第3列中的文本区域。在

我还想在底部创建一个带有关闭按钮的边框,但这根本没有显示出来。在

你能告诉我我做错了什么吗?在

import Tkinter
from Tkinter import *
from ttk import Frame, Button, Style


class KarateSyllabus(Frame):
    """A program that displays karate grading syllabi"""


  #define the constructor
    def __init__(self, parent):
        Frame.__init__(self, parent)   

        self.parent = parent

        self.initUI()


    #define the GUI
    def initUI(self):

        #define the basic parameters of the window
        self.parent.title("Karate Syllabus")
        self.style = Style()
        self.style.theme_use("default")
        #self.parent.geometry("500x500")
        self.parent.config(background = "black")
        self.parent.wm_iconbitmap("favicon.ico")
        self.grid()


        #create the buttons for the syllabus
        button1 = Tkinter.Button(self, text = "White Belt", bg = "white",         height=1, width =10).grid(row=0, column=0, pady=4, padx=10, sticky=N)
        button2 = Tkinter.Button(self, text = "Red Belt",   bg="red", height=1, width =10).grid(row=1,column=0, pady=4, padx=10,  sticky=N )
        button3 = Tkinter.Button(self, text = "Orange Belt",bg="orange", height=1, width =10).grid(row=2,column=0, pady=4, padx=10,  sticky=N)
        button4 = Tkinter.Button(self, text = "Yellow Belt",bg="yellow", height=1, width =10).grid(row=3, column=0, pady=4, padx=10,  sticky=N)
        button5 = Tkinter.Button(self, text = "Green Belt", bg="green", height=1, width =10).grid(row=4, column=0, pady=4, padx=10,  sticky=N)
        button6 = Tkinter.Button(self, text = "Purple Belt",bg="purple", height=1, width =10).grid(row=5, column=0, pady=4, padx=10,  sticky=N)
        button7 = Tkinter.Button(self, text = "Brown Belt", bg="brown", height=1, width =10).grid(row=6, column=0, pady=4, padx=10,  sticky=N)
        button8 = Tkinter.Button(self, text = "Black Belt", bg="black", foreground="white", height=1, width =10).grid(row=7, column=0, pady=2, padx=10,  sticky=N)


        #create the three text areas to display the text and according labels
        BasicsLabel = Label(self, text="Basics:").grid(row =0, column =2)
        BasicTextArea = Text(self, width=50, height=6, takefocus=0)
        BasicTextArea.grid(row=0, column=3, padx=10, pady=2)
        BasicTextArea.config(font =("Arial",10), bg="grey", wrap = WORD)

        KataLabel = Label(self, text="Kata:").grid(row =2, column =2)
        KataTextArea = Text(self, width=50, height=6, takefocus=0)
        KataTextArea.grid(row=2, column=3, padx=30, pady=2)
        KataTextArea.config(font =("Arial",10), bg="grey")

        KumiteLabel = Label(self, text="Kumite:").grid(row =3, column =2)
        KumiteTextArea = Text(self, width=50, height=6, takefocus=0)
        KumiteTextArea.grid(row=3, column=3, padx=10, pady=2)
        KumiteTextArea.config(font =("Arial",10), bg="grey")

        #create the second frame for the bottom with the close button
        frame = Frame(self, relief=RAISED, borderwidth=1)
        frame.grid(row=8, column= 1)

        closeButton = Button(self, text="Exit")
        closeButton.grid(row = 8, column = 3)



def main():

    root = Tk()
    app = KarateSyllabus(root)
    root.mainloop()


if __name__ == '__main__':
    main()  

Tags: thetextselftkintercolumnbuttonwidthgrid
2条回答

听起来你不需要使用网格,因为你没有创建网格。听起来你希望每个列的垂直间距相等,这就排除了类似网格的布局。在

你要创建三个列,所以我将从底部开始打包一个框架,然后在主窗口中从左到右打包三个垂直的框架。在

接下来,从上到下,在最左边的框架中打包颜色按钮。有了正确的选项,它们将均匀分布(尽管你也可以使用网格,如果你想的话)。在

最后,对其他两列使用完全相同的技术-从上到下打包所有内容,让每个列展开以填充分配给它们的区域。在

您应该至少使用一个框架来分组所有左按钮,并使用另一个框架来分组退出按钮,如以下代码所示:

import Tkinter
from ttk import Frame, Button, Style

class KarateSyllabus(Frame):
    """A program that displays karate grading syllabi"""

  #define the constructor
    def __init__(self, parent):
        Frame.__init__(self, parent)   
        self.parent = parent
        self.initUI()

    #define the GUI
    def initUI(self):
        #define the basic parameters of the window
        self.parent.title("Karate Syllabus")
        self.style = Style()
        self.style.theme_use("default")
        #self.parent.geometry("500x500")
        self.parent.config(background = "black")
        self.parent.wm_iconbitmap("favicon.ico")
        self.grid(sticky=Tkinter.NSEW)

        button_panel = Frame(self)

        #create the buttons for the syllabus
        button1 = Tkinter.Button(button_panel, text="White Belt",  bg="white",  height=1, width =10).grid(row=0, column=0, pady=4, padx=10, sticky=Tkinter.N)
        button2 = Tkinter.Button(button_panel, text="Red Belt",    bg="red",    height=1, width =10).grid(row=1, column=0, pady=4, padx=10, sticky=Tkinter.N)
        button3 = Tkinter.Button(button_panel, text="Orange Belt", bg="orange", height=1, width =10).grid(row=2, column=0, pady=4, padx=10, sticky=Tkinter.N)
        button4 = Tkinter.Button(button_panel, text="Yellow Belt", bg="yellow", height=1, width =10).grid(row=3, column=0, pady=4, padx=10, sticky=Tkinter.N)
        button5 = Tkinter.Button(button_panel, text="Green Belt",  bg="green",  height=1, width =10).grid(row=4, column=0, pady=4, padx=10, sticky=Tkinter.N)
        button6 = Tkinter.Button(button_panel, text="Purple Belt", bg="purple", height=1, width =10).grid(row=5, column=0, pady=4, padx=10, sticky=Tkinter.N)
        button7 = Tkinter.Button(button_panel, text="Brown Belt",  bg="brown",  height=1, width =10).grid(row=6, column=0, pady=4, padx=10, sticky=Tkinter.N)
        button8 = Tkinter.Button(button_panel, text="Black Belt",  bg="black",  height=1, width =10, foreground="white").grid(row=7, column=0, pady=2, padx=10, sticky=Tkinter.N)

        button_panel.grid(row=0, column=0, rowspan=3, sticky=Tkinter.N)

        #create the three text areas to display the text and according labels
        BasicsLabel = Tkinter.Label(self, text="Basics:").grid(row=0, column=1, sticky=Tkinter.N)
        BasicTextArea = Tkinter.Text(self, width=50, height=6, takefocus=0)
        BasicTextArea.grid(row=0, column=2, padx=10, pady=2, sticky=Tkinter.NSEW)
        BasicTextArea.config(font=("Arial",10), bg="grey", wrap=Tkinter.WORD)

        KataLabel = Tkinter.Label(self, text="Kata:").grid(row=1, column=1, sticky=Tkinter.N)
        KataTextArea = Tkinter.Text(self, width=50, height=6, takefocus=0)
        KataTextArea.grid(row=1, column=2, padx=10, pady=2, sticky=Tkinter.NSEW)
        KataTextArea.config(font =("Arial",10), bg="grey")

        KumiteLabel = Tkinter.Label(self, text="Kumite:").grid(row=2, column=1, sticky=Tkinter.N)
        KumiteTextArea = Tkinter.Text(self, width=50, height=6, takefocus=0)
        KumiteTextArea.grid(row=2, column=2, padx=10, pady=2, sticky=Tkinter.NSEW)
        KumiteTextArea.config(font=("Arial",10), bg="grey")

        #create the second frame for the bottom with the close button
        close_frame = Tkinter.Frame(self, relief=Tkinter.RAISED, borderwidth=2)
        close_frame.grid(row=3, column=0, columnspan=3, sticky=Tkinter.EW)
        close_frame.columnconfigure(0, weight=1)

        closeButton = Tkinter.Button(close_frame, text="Exit", command=self.quit)
        # Move 'Exit' to the right. Comment out next line to leave it centered.
        closeButton.grid(sticky=Tkinter.E)

        self.rowconfigure(0, weight=1)
        self.rowconfigure(1, weight=1)
        self.rowconfigure(2, weight=1)
        # Leave row 3 (close_frame) non-expandable.

        # Leave columns 1 and 2 (button_panel and labels) non-expandable.
        self.columnconfigure(2, weight=1)

        self.parent.rowconfigure(0, weight=1)
        self.parent.columnconfigure(0, weight=1)


def main():
    root = Tkinter.Tk()
    app = KarateSyllabus(root)
    root.mainloop()


if __name__ == '__main__':
    main()

相关问题 更多 >