【问题标题】:Closing windows of tkinter关闭 tkinter 的窗口
【发布时间】:2017-09-04 04:50:25
【问题描述】:

我是 tkinter 的新手,想根据显示的标志做一个 UI。 基本上,我想关闭一个窗口并以当前状态打开另一个窗口,或者删除文本并以当前状态显示另一个文本。

class App(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.start()

    def callback(self):
        self.root.quit()

    def run(self):
        self.root = tk.Tk()
        self.root.protocol("WM_DELETE_WINDOW", self.callback)

        label = tk.Label(self.root, text="Start Initialization")
        label.pack()

        self.root.mainloop()


class QQQ:

    def quit(self):
        self.delete(1.0,END)



class Appo(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.start()

    def callback(self):
        self.root.quit()

    def run(self):
        self.root = tk.Tk()
        self.root.protocol("WM_DELETE_WINDOW", self.callback)

        label = tk.Label(self.root, text="Initialization ended")
        label.pack()

        self.root.mainloop()


for i in range(100000):

    time.sleep(0.5)

    print(i)

    if(i==1):

        app = App()

        time.sleep(1)

        qqq=QQQ()

    if(i==10):

        app=Appo()

【问题讨论】:

  • 您的问题/疑问是什么? (建议,去掉代码的双倍行距。)
  • 在线程函数中,您可以使用while循环并检查您可以在主线程中更改的条件。
  • 我正在考虑实时更改短信。

标签: python python-2.7 python-3.x tkinter raspberry-pi3


【解决方案1】:

如果您只想更改标签的文本,请在标签上使用config 方法。 AppAppoQQQ 类以及 for 循环可以组合成一个类:

import Tkinter as tk #Python 2
#import tkinter as tk #Python 3
import threading
import time


class App(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.root = tk.Tk()
        self.start()

    def callback(self):
        self.root.quit()

    def run(self):
        self.root.protocol("WM_DELETE_WINDOW", self.callback)

        label = tk.Label(self.root, text="Initial Text") # You can use text=""
        label.pack()

        for i in range(100000):
            time.sleep(0.5)
            print (i)
            if i == 1:
                label.config(text="Start Initialization")
                time.sleep(1)
                label.config(text="")
            if i == 10:
                label.config(text="Initialization ended")
        #self.root.mainloop()

app = App()
app.root.mainloop()

使用 tkinter 的 after 方法进行时间延迟可能会更好,而不是 time.sleep()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-26
    • 2010-09-11
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多