简体   繁体   English

Python tkinter - 文本未显示(GUI)

[英]Python tkinter - Text not showing (GUI)

I have no bug errors and I was wondering why 'TIMER' is not showing up in the GUI when I run it.我没有错误错误,我想知道为什么运行它时 GUI 中没有显示“TIMER”。 It just shows a white box.它只显示一个白框。 I`ve tried searching the forums for an issue similar to mine but I failed to find any.我试过在论坛上搜索与我类似的问题,但没有找到。 CODE:代码:

import tkinter


class study_timer:
    def __init__(self, master):
        self.master = master
        self.mainframe = tkinter.Frame(self.master, bg='white')
        self.mainframe.pack(fill = tkinter.BOTH, expand=True)

        self.build_grid()
        self.build_banner()

    def build_grid(self):
        self.mainframe.columnconfigure(0, weight=1)
        self.mainframe.rowconfigure(0, weight=0)
        self.mainframe.rowconfigure(0, weight=1)
        self.mainframe.rowconfigure(0, weight=0)

    def build_banner(self):
        banner = tkinter.Label(
            self.mainframe,
            bg='black',
            text='TIMER',
            fg='white',
            font=('Ravie Regular', 30)
        )
        banner.grid(
            row=0, column=0,
            stick='ew',
            padx=10, pady=10
        )

    if __name__ == "__main__":
        root = tkinter.Tk()
        root.mainloop()

You should instantiate an object of the class if you want to run the functions that you defined.如果你想运行你定义的函数,你应该实例化一个类的对象。 The functions are called from constructor( init ) in your class structure.这些函数是从类结构中的构造函数( init )调用的。

Second, if statement's indentation is wrong.其次,如果语句的缩进是错误的。

Third, you should send the root object to init function as parameter.第三,您应该将根对象作为参数发送给 init 函数。

This will work这将工作

import tkinter


class study_timer:
    def __init__(self, master):
        self.master = master
        self.mainframe = tkinter.Frame(self.master, bg='white')
        self.mainframe.pack(fill = tkinter.BOTH, expand=True)

        self.build_grid()
        self.build_banner()

    def build_grid(self):
        self.mainframe.columnconfigure(0, weight=1)
        self.mainframe.rowconfigure(0, weight=0)
        self.mainframe.rowconfigure(0, weight=1)
        self.mainframe.rowconfigure(0, weight=0)

    def build_banner(self):
        banner = tkinter.Label(
            self.mainframe,
            bg='black',
            text='TIMER',
            fg='white',
            font=('Ravie Regular', 30)
        )
        banner.grid(
            row=0, column=0,
            stick='ew',
            padx=10, pady=10
        )

if __name__ == "__main__":
    root = tkinter.Tk()
    ss = study_timer(root)
    root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM