【问题标题】:Destroying and Recreating a TK Window in Python在 Python 中销毁和重新创建 TK 窗口
【发布时间】:2013-03-16 10:37:06
【问题描述】:

我使用 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

如何在避免此错误的情况下创建、销毁并再次创建窗口?

【问题讨论】:

  • 抱歉,关于您的问题,您提供的细节非常少,因此几乎不可能有人帮助您。首先,您使用的是哪个窗口框架?派格特克? PyQt? wxPython?特金特?还有什么?最重要的是,请向我们展示您正在运行的代码。不看你的代码就很难帮助你。
  • 我正在使用 Tkinter 对不起,我的意思是我需要创建一个新窗口而不创建另一个线程

标签: python multithreading window tk


【解决方案1】:

如果你使用线程,你可以这样做:-

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

而且它没有显示任何错误。

【讨论】:

  • 我在代码中添加了 def run(self): 因为我忘了放它,它显示错误
猜你喜欢
  • 1970-01-01
  • 2019-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多