我使用python在线程中创建一个根窗口。接下来我需要销毁它并创建一个新窗口,但是错误说明线程只能启动一次。如何开始新窗口表单?
class SignUp(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.mw=Tkinter.Tk()
self.mw.geometry("800x600""+290+50")
self.mw.title("Registration Form")
self.mw.resizable(width=False,height=False)
self.regs=Tkinter.Button(self.canvas, text= "Log-in",font="{Arial} 12 {bold}",bg='black',fg='white',command=self.login)
self.regs.place(rely=0.82,relx=0.1,relwidth=0.30)
self.start()
def login(self):
self.mw.destroy()
self.mw.geometry("650x700""+350+20")
self.mw.title("Py cos")
self.mw.resizable(width=False,height=False)
self.canvas= Tkinter.Canvas(self.mw)
self.canvas.pack(expand='yes',fil='both')
self.photo= Tkinter.PhotoImage(file='Image/vote.gif')
self.canvas.create_image(0,0,image=self.photo,anchor='nw')
def run(self):
self.client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.client.connect(('localhost',500))
#self.client.connect(('169.254.57.141',500))
self.rec=self.client.recv(1024)
self.canvas.create_text(105,595,text=self.rec, font="{Arial} 8 {bold}", fill = 'yellow')
if __name__=="__main__":
SignUp().mw.mainloop()
这段代码给了我
RuntimeError: threads can only be started once
如何在避免此错误的同时创建窗口,销毁窗口并再次创建窗口?
答案 0 :(得分:0)
如果您使用线程,您可以执行以下操作: -
from tkinter import *
from threading import Thread
class App():
def __init__(self):
self.g=Tk()
self.th=Thread(target=self.g.mainloop)
self.th.start()
def destroy(self):
self.g.destroy()
如果你不使用线程,你可以在你的应用程序中放置一个中止按钮: -
class App():
def __init__(self):
self.g=Tk()
self.bu=Button(self.g,text='Abort',command=self.Abort)
self.bu.pack()
self.g.mainloop()
def Abort(self):
self.g.destroy()
我认为你的代码应该是: -
class SignUp(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.mw=Tkinter.Tk()
self.mw.geometry("800x600""+290+50")
self.mw.title("Registration Form")
self.mw.resizable(width=False,height=False)
self.regs=Tkinter.Button(self.mw, text= "Log-in",font="{Arial} 12 {bold}",bg='black',fg='white',command=self.login)
self.regs.place(rely=0.82,relx=0.1,relwidth=0.30)
self.start()
def login(self):
self.mw.destroy()
self.mw=Tkinter.Tk()
self.mw.geometry("650x700""+350+20")
self.mw.title("Py cos")
self.mw.resizable(width=False,height=False)
self.canvas= Tkinter.Canvas(self.mw)
self.canvas.pack(expand='yes',fill='both')
self.photo= Tkinter.PhotoImage(file='Image/vote.gif')
self.canvas.create_image(0,0,image=self.photo,anchor='nw')
self.mw.mainloop()
if __name__=="__main__":
SignUp().mw.mainloop()
它没有显示任何错误。