在 Tkinter 等待时间的根目录中显示新信息作为多进程
Show new info in root of Tkinter waiting time as a multiprocess
在 python 中,如果给定条件,我想将消息 2 添加到 Tkinter window。在此示例中,我将 X(时间)的值用作多进程,但一旦进入 Tkinter 根循环,我就无法提供新消息 (2)。
from tkinter import *
import time as t
from threading import Thread
def time1():
global x
x = 0
while x<3:
t.sleep(1)
x += 1
def gui():
root = Tk()
Label(root, text=("Message 1")).pack()
if x == 2:
Label(root, text=("Message 2")).pack()
root.mainloop()
generator = Thread(target=time1)
processor1 = Thread(target=gui)
generator.start()
processor1.start()
generator.join()
在您的 gui
函数中,您仅在输入 mainloop
之前测试 x
一次 的值。如果x
后面变成2,则不会发生任何事情。
试试这个:
import tkinter as tk
import time
from threading import Thread
def time1():
"""
Add a second Label after a certain condition is met.
"""
x = 0
while x < 3:
time.sleep(1)
x += 1
tk.Label(root, text="Message 2").pack()
if __name__ == "__main__":
root = tk.Tk()
tk.Label(root, text="Message 1").pack()
generator = Thread(target=time1)
generator.start()
root.mainloop()
来自 Python 教程:
Note that in general the practice of importing *
from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.
GUI 运行 来自主线程,这被认为是 tkinter
的良好做法。这是为了确保在 tkinter
未构建多线程支持的情况下一切正常。
在 python 中,如果给定条件,我想将消息 2 添加到 Tkinter window。在此示例中,我将 X(时间)的值用作多进程,但一旦进入 Tkinter 根循环,我就无法提供新消息 (2)。
from tkinter import *
import time as t
from threading import Thread
def time1():
global x
x = 0
while x<3:
t.sleep(1)
x += 1
def gui():
root = Tk()
Label(root, text=("Message 1")).pack()
if x == 2:
Label(root, text=("Message 2")).pack()
root.mainloop()
generator = Thread(target=time1)
processor1 = Thread(target=gui)
generator.start()
processor1.start()
generator.join()
在您的 gui
函数中,您仅在输入 mainloop
之前测试 x
一次 的值。如果x
后面变成2,则不会发生任何事情。
试试这个:
import tkinter as tk
import time
from threading import Thread
def time1():
"""
Add a second Label after a certain condition is met.
"""
x = 0
while x < 3:
time.sleep(1)
x += 1
tk.Label(root, text="Message 2").pack()
if __name__ == "__main__":
root = tk.Tk()
tk.Label(root, text="Message 1").pack()
generator = Thread(target=time1)
generator.start()
root.mainloop()
来自 Python 教程:
Note that in general the practice of importing
*
from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.
GUI 运行 来自主线程,这被认为是 tkinter
的良好做法。这是为了确保在 tkinter
未构建多线程支持的情况下一切正常。