我在执行以下脚本时没有得到任何提示窗口

时间:2018-10-18 11:29:22

标签: python python-3.x tkinter

from tkinter import *

class Application(Frame):

  def __init__(self, master):

    Frame.__init__(self,master)
    self.grid()
    self.create_widgets()


  def create_widgets(self):

    self.instruction = Label(self, text="Enter password")
    self.instruction.grid(row=0, cloumn=0, cloumnspan=2, sticky=W)

    self.password = Entry(self)
    self.password.grid(row=1, column=1, sticky=W)

    self.submit_button = Button(self, text="submit", command=self.reveal)
    self.submit_button.grid(row=2, column=0, sticky=W)

    self.text = Text(self, width=35, height=5, wrap=WORD)
    self.text.grid(row=3, column=0, columnspan=2, sticky=W)


def reveal(self):

    content = self.password.get()

    if content == "password":
        message = "You have access to something special"

    else:
        message = "Access Denined"
        self.text.insert(0.0, message)

    root = Tk()
    menubar = Menu(root)

    root.geometry("450x450+500+300")
    root.title("Change Creation")
    filemenu = Menu(menubar, tearoff=0)

    filemenu.add_command(label="Close", command = close)

    menubar.add_cascade(label="File", menu=filemenu)

    root.title("Password")
    root.geometry("250x150")
    app = Application(root)


    root.mainloop()

在执行上述代码时,我没有得到任何提示或任何错误。请帮助我寻找解决方法

1 个答案:

答案 0 :(得分:0)

您有几个缩进错误和错字。

我不确定您是怎么得到错误的,因为在尝试运行您的代码时,我必须修复至少3个错误。如果您使用的是Python的默认IDLE,则建议升级到PyCharm或Eclipse Pydev。它们将提供适当的回溯错误以进行调试。

我已经清理了您的代码。确保检查拼写。您有cloumn而不是columncloumnspan而不是columnspancommand = close将导致错误,因为不存在称为close的方法或函数。

from tkinter import *

class Application(Frame):
    def __init__(self, master):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.instruction = Label(self, text="Enter password")
        self.instruction.grid(row=0, column=0, columnspan=2, sticky=W)

        self.password = Entry(self)
        self.password.grid(row=1, column=1, sticky=W)

        self.submit_button = Button(self, text="submit", command=self.reveal)
        self.submit_button.grid(row=2, column=0, sticky=W)

        self.text = Text(self, width=35, height=5, wrap=WORD)
        self.text.grid(row=3, column=0, columnspan=2, sticky=W)

    def reveal(self):
        content = self.password.get()
        if content == "password":
            message = "You have access to something special"
        else:
            message = "Access Denined"
            self.text.insert(0.0, message)

root = Tk()
menubar = Menu(root)

root.geometry("450x450+500+300")
root.title("Change Creation")
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Close")
menubar.add_cascade(label="File", menu=filemenu)

root.title("Password")
root.geometry("250x150")
app = Application(root)


root.mainloop()