Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tkinter-文本不显示(GUI)_Python_Python 3.x_Tkinter - Fatal编程技术网

Python tkinter-文本不显示(GUI)

Python tkinter-文本不显示(GUI),python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我没有bug错误,我想知道为什么运行时GUI中没有显示“TIMER”。它只是显示了一个白色的框。我试着在论坛上搜索与我类似的问题,但没有找到。 代码: 如果要运行定义的函数,应该实例化类的对象。这些函数是从类结构中的constructorinit调用的 第二,如果语句的缩进是错误的 第三,您应该将根对象作为参数发送给init函数 这会奏效的 import tkinter class study_timer: def __init__(self, master): se

我没有bug错误,我想知道为什么运行时GUI中没有显示“TIMER”。它只是显示了一个白色的框。我试着在论坛上搜索与我类似的问题,但没有找到。 代码:


如果要运行定义的函数,应该实例化类的对象。这些函数是从类结构中的constructorinit调用的

第二,如果语句的缩进是错误的

第三,您应该将根对象作为参数发送给init函数

这会奏效的

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

你在任何地方实例化过这个类吗?这就是全部密码吗?这里的缩进是否与实际代码匹配?你的实际代码使用命名参数stick而不是stick吗?我只开始使用python,所以我不太明白你所说的“实例化这个类”是什么意思,你介意解释一下这意味着什么吗?是的,我的缩进与实际代码匹配。我的代码使用stick,但我尝试更改为stick,但在运行时没有效果。您定义了一个类,然后不创建该类的对象。如果If\uuuu name\uuuuu。。。块位于从不实例化的类内,在开始实例化该类之前,不会执行该块。通常,该块放置在最外面的缩进级别。这是您的意思吗?课堂学习对象:不,那只是定义它的另一种方式。我的意思是创建该类的对象,例如my_study_timer=study_timeroot。如果你从来没有这样做过,那么这个类中的任何内容都不会被执行。谢谢你,这已经成功了!我刚刚修复了if的缩进,它成功了。
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()