2013-03-16 858 views
-2

我使用python在線程中創建一個根窗口。接下來是我需要銷燬它並創建一個新窗口,但錯誤是說只能啓動一次線程。我如何開始一個新的窗體?在Python中銷燬和重新創建一個TK窗口

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 

我怎樣才能創建一個窗口,摧毀它,然後再重新創建,同時避免這個錯誤?

+2

對不起,您提供了有關您的問題的*非常少的細節*,所以任何人都無法幫助您。首先,您使用的是哪個窗口框架? PyGTK的? PyQt的? wxPython的? Tkinter的?還有別的嗎?最重要的是,請**向我們展示您正在運行的代碼**。沒有看到你的代碼就很難幫助你。 – 2013-03-16 10:54:32

+0

即時通訊使用Tkinter對不起,我的意思是我需要創建一個新的窗口,而不使另一個線程 – Kit 2013-03-16 11:02:59

回答

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

而且它不顯示任何錯誤。

+0

我添加了def運行(自我):在代碼中,因爲我忘了把它,它顯示錯誤 – Kit 2013-03-16 12:17:36