Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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 TCL错误:can';t调用;“销毁”;命令:应用程序已被销毁_Python_Python 2.7_Tkinter - Fatal编程技术网

Python TCL错误:can';t调用;“销毁”;命令:应用程序已被销毁

Python TCL错误:can';t调用;“销毁”;命令:应用程序已被销毁,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我是python初学者。尝试制作一个新按钮来关闭窗口。我收到了错误消息: Tkinter回调中的异常 回溯(最近一次调用上次):文件 “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py”, 第1536行,在调用中 返回self.func(*args)文件“tk_cp_successful.py”,第138行,按按钮 self.root.destroy()文件“/Syst

我是python初学者。尝试制作一个新按钮来关闭窗口。我收到了错误消息:

Tkinter回调中的异常 回溯(最近一次调用上次):文件 “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py”, 第1536行,在调用中 返回self.func(*args)文件“tk_cp_successful.py”,第138行,按按钮 self.root.destroy()文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib tk/Tkinter.py”, 第1859行,在销毁中 self.tk.call('destroy',self.w)TclError:无法调用“destroy”命令:应用程序已被销毁


你的代码有很多问题

  • 缩进误差
  • 混合
    grid()
    pack()
  • 您是将tkinter作为tk导入还是从tkinter import*导入
    ,即
    self.root=tk.tk()
    导入为tk
    )或
    label\u 1=标签(self,text=“Username”)
    来自tkinter导入*
  • 程序中没有
    mainloop
  • 在类中使用global是不必要的,而且样式很差
  • 在任何情况下,下面修改过的代码都会运行,希望它能有所帮助

    import sys
    if sys.version_info[0] < 3:
        import Tkinter as tk     ## Python 2.x
    else:
        import tkinter as tk     ## Python 3.x
    
    class LoginPage():
       def __init__(self):
          self.root=tk.Tk()
          label = tk.Label(self.root, text="Welcome to VISA Login Page",fg="blue")
          label.grid(row=0)
    
          label_1 = tk.Label(self.root, text="Username")
          label_2 = tk.Label(self.root, text="Password")
          self.entry_1 = tk.Entry(self.root)
          self.entry_2 = tk.Entry(self.root, show="*")
          label_1.grid(row=1, sticky="e")
          label_2.grid(row=2, sticky="e")
          self.entry_1.grid(row=1, column=1)
          self.entry_2.grid(row=2, column=1)
    
          ## doesn't do anything at this time
          ##checkbox = tk.Checkbutton(self.root, text="Keep me logged in")
          ##checkbox.grid(row=3, columnspan=2)
    
          logbtn = tk.Button(self.root, text="Login", command = self._login_btn_clickked)
          logbtn.grid(row=9, columnspan=2)
          myButton = tk.Button(self.root, text="Exit",command = self.buttonPushed)
          myButton.grid(row=10)
    
          self.root.mainloop()
    
       def buttonPushed(self):
          self.root.destroy()
    
       def _login_btn_clickked(self):
          #print("Clicked")
          username = self.entry_1.get()
          password = self.entry_2.get()
    
          #print(username, password)
    
          if username == "test" and password == "test":
              print "OK login"
              #box.showinfo("Login info", "Welcome Tester")
              #button1 = ttk.Button(self.root, text="Please click, Welcome to login!!!",
              #           command=lambda: self.controller.show_frame(StartPage))
              #button1.pack()
          else:
              #box.showerror("Login failed", "Incorrect username")
              print "Error"
    
    LP=LoginPage()
    
    导入系统 如果系统版本信息[0]<3: 将Tkinter作为tk##Python 2.x导入 其他: 将tkinter作为tk##Python 3.x导入 类LoginPage(): 定义初始化(自): self.root=tk.tk() label=tk.label(self.root,text=“欢迎来到VISA登录页面”,fg=“蓝色”) label.grid(行=0) label_1=tk.label(self.root,text=“Username”) label_2=tk.label(self.root,text=“Password”) self.entry_1=tk.entry(self.root) self.entry_2=tk.entry(self.root,show=“*”) 标签1.网格(行=1,sticky=“e”) 标签2.网格(行=2,sticky=“e”) self.entry_1.grid(行=1,列=1) self.entry_2.grid(行=2,列=1) ##现在什么都不做 ##checkbox=tk.Checkbutton(self.root,text=“让我登录”) ##checkbox.grid(行=3,列span=2) logbtn=tk.按钮(self.root,text=“Login”,command=self.\u Login\u btn\u clickked) logbtn.grid(行=9,列span=2) myButton=tk.Button(self.root,text=“Exit”,command=self.buttonPushed) myButton.grid(行=10) self.root.mainloop() def按钮按下(自): self.root.destroy() 定义、登录、点击(自我): #打印(“单击”) username=self.entry_1.get() 密码=self.entry_2.get() #打印(用户名、密码) 如果用户名==“测试”和密码==“测试”: 打印“确定登录” #box.showinfo(“登录信息”、“欢迎测试人员”) #button1=ttk.Button(self.root,text=“请单击,欢迎登录!!!”, #command=lambda:self.controller.show_frame(起始页)) #按钮1.pack() 其他: #框。淋浴错误(“登录失败”,“用户名不正确”) 打印“错误” LP=LoginPage()
    您的代码有很多问题

  • 缩进误差
  • 混合
    grid()
    pack()
  • 您是将tkinter作为tk导入还是从tkinter import*导入
    ,即
    self.root=tk.tk()
    导入为tk
    )或
    label\u 1=标签(self,text=“Username”)
    来自tkinter导入*
  • 程序中没有
    mainloop
  • 在类中使用global是不必要的,而且样式很差
  • 在任何情况下,下面修改过的代码都会运行,希望它能有所帮助

    import sys
    if sys.version_info[0] < 3:
        import Tkinter as tk     ## Python 2.x
    else:
        import tkinter as tk     ## Python 3.x
    
    class LoginPage():
       def __init__(self):
          self.root=tk.Tk()
          label = tk.Label(self.root, text="Welcome to VISA Login Page",fg="blue")
          label.grid(row=0)
    
          label_1 = tk.Label(self.root, text="Username")
          label_2 = tk.Label(self.root, text="Password")
          self.entry_1 = tk.Entry(self.root)
          self.entry_2 = tk.Entry(self.root, show="*")
          label_1.grid(row=1, sticky="e")
          label_2.grid(row=2, sticky="e")
          self.entry_1.grid(row=1, column=1)
          self.entry_2.grid(row=2, column=1)
    
          ## doesn't do anything at this time
          ##checkbox = tk.Checkbutton(self.root, text="Keep me logged in")
          ##checkbox.grid(row=3, columnspan=2)
    
          logbtn = tk.Button(self.root, text="Login", command = self._login_btn_clickked)
          logbtn.grid(row=9, columnspan=2)
          myButton = tk.Button(self.root, text="Exit",command = self.buttonPushed)
          myButton.grid(row=10)
    
          self.root.mainloop()
    
       def buttonPushed(self):
          self.root.destroy()
    
       def _login_btn_clickked(self):
          #print("Clicked")
          username = self.entry_1.get()
          password = self.entry_2.get()
    
          #print(username, password)
    
          if username == "test" and password == "test":
              print "OK login"
              #box.showinfo("Login info", "Welcome Tester")
              #button1 = ttk.Button(self.root, text="Please click, Welcome to login!!!",
              #           command=lambda: self.controller.show_frame(StartPage))
              #button1.pack()
          else:
              #box.showerror("Login failed", "Incorrect username")
              print "Error"
    
    LP=LoginPage()
    
    导入系统 如果系统版本信息[0]<3: 将Tkinter作为tk##Python 2.x导入 其他: 将tkinter作为tk##Python 3.x导入 类LoginPage(): 定义初始化(自): self.root=tk.tk() label=tk.label(self.root,text=“欢迎来到VISA登录页面”,fg=“蓝色”) label.grid(行=0) label_1=tk.label(self.root,text=“Username”) label_2=tk.label(self.root,text=“Password”) self.entry_1=tk.entry(self.root) self.entry_2=tk.entry(self.root,show=“*”) 标签1.网格(行=1,sticky=“e”) 标签2.网格(行=2,sticky=“e”) self.entry_1.grid(行=1,列=1) self.entry_2.grid(行=2,列=1) ##现在什么都不做 ##checkbox=tk.Checkbutton(self.root,text=“让我登录”) ##checkbox.grid(行=3,列span=2) logbtn=tk.按钮(self.root,text=“Login”,command=self.\u Login\u btn\u clickked) logbtn.grid(行=9,列span=2) myButton=tk.Button(self.root,text=“Exit”,command=self.buttonPushed) myButton.grid(行=10) self.root.mainloop() def按钮按下(自): self.root.destroy() 定义、登录、点击(自我): #打印(“单击”) username=self.entry_1.get() 密码=self.entry_2.get() #打印(用户名、密码) 如果用户名==“测试”和密码==“测试”: 打印“确定登录” #box.showinfo(“登录信息”、“欢迎测试人员”) #button1=ttk.Button(self.root,text=“请单击,欢迎登录!!!”, #command=lambda:self.controller.show_frame(起始页)) #按钮1.pack() 其他: #框。淋浴错误(“登录失败”,“用户名不正确”) 打印“错误” LP=LoginPage()
    忽略代码中的所有其他问题,我前几天也遇到了同样的问题。调用
    self.root.destroy()
    时,Tkinter将退出
    root.main循环。然后在调用
    root.mainloop
    的位置之后,您可能会调用
    root.destroy
    。这意味着您试图销毁两次,这是导致错误的原因。处理此问题的一种方法是让
    异常
    以静默方式通过(尽管通常这不是一种好的做法):

    我可能错了,但这是我唯一能想象的
    try:
        root.destroy()
    except:
        pass
    
    import sys
    
    B=tk.Button(self.root,text="quit",command=lambda:sys.exit())
    B.grid()