Tkinter 小部件未出现 [英] Tkinter widgets not appearing

查看:74
本文介绍了Tkinter 小部件未出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试我正在编写的应用程序,但我得到了一个空白窗口,没有任何小部件.

I was testing an app I was writing but I just get a blank window and no widgets.

from Tkinter import*
class App(Frame):

def _init_(self, master):

    frame = Frame(master)
    frane.pack()

    self.instruction = Label(frame, text = 'Password:')
    self.instruction.pack()

    self.button = Button(frame, text = 'Enter', command = self.reveal)
    self.button.pack()


root = Tk()
root.title('Password')
root.geometry('350x250')
App(root)
root.mainloop()

推荐答案

你有几个错别字.第一个是在构造方法的名称中:

You have a couple of typos. The first is in the name of the constructor method:

def _init_(self, master):

应阅读:

def __init__(self, master):

注意双下划线 - 请参阅文档用于 Python 对象.

Note the double underscore - see the docs for Python objects.

第二个在你的构造函数中:

The second is inside your constructor:

frane.pack()

您还缺少 App 类中名为reveal"的方法的声明:

You're also missing a declaration for a method named 'reveal' in your App class:

self.button = Button(frame, text="Enter", command=self.reveal)

工作示例如下:

from Tkinter import *

class App(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.pack()

        frame = Frame()
        frame.pack()

        self.instruction = Label(frame, text="Password:")
        self.instruction.pack()

        self.button = Button(frame, text="Enter", command=self.reveal)
        self.button.pack()


    def reveal(self):
        # Do something.
        pass


root = Tk()
root.title("Password")
root.geometry("350x250")
App(root)
root.mainloop()

另见:Tkinter 文档.

这篇关于Tkinter 小部件未出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆