大佬教程收集整理的这篇文章主要介绍了使用 Python tkinter 显示数据流,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 tkinter 实现数据流显示器。我有一个计算一些数据的程序,我想编程以在数据可用时在窗口中以块的形式显示这些数据(也称为数据流)。
我想显示 msg1 并在显示 msg2 之前等待 12 秒(完全人为的延迟,代表我的计算的预期延迟,这是可变的)。程序所做的是等待 12 秒,然后将两条消息一起显示。
from tkinter import *
import time
window = Tk()
msg1 = Label(window,text="display Part 1")
msg1.pack()
time.sleep(12)
msg2 = Label(window,text="display Part 2")
msg2.pack()
window.geometry("600x600")
window.mainloop()
非常感谢您的意见!
GUI 不像 input()
那样工作 - Label
不会立即创建小部件,但它会通知要显示的小部件,然后 @H_693_9@mainloop 创建窗口并显示它。所以你在它甚至创建窗口之前sleep
。
其他问题可能是 @H_693_9@mainloop 必须一直运行才能从系统获取键/鼠标事件,将事件发送到小部件并在窗口中更新/重绘小部件。如果你使用 sleep
那么它会停止 @H_693_9@mainloop 并且它会冻结窗口。
延迟后可以使用root.after(millisecond,function)
来执行函数。
import tkinter as tk # PEP8: `import *` is not preferred
import time
# --- functions ---
def add_label():
msg2 = tk.Label(window,text="Display Part 2")
msg2.pack()
# --- main ---
window = tk.Tk()
window.geometry("600x600")
msg1 = tk.Label(window,text="Display Part 1")
msg1.pack()
window.after(12000,add_label) # 12000ms = 12s
window.mainloop()
PEP 8 -- Style Guide for Python Code
顺便说一句:
如果您想同时进行一些计算,则必须在单独的线程中进行。但是您不能在其他线程中使用小部件,因此您可能需要使用 queue
从其他线程发送文本,而主线程将需要使用 after
定期检查 queue
并添加新文本到图形用户界面。
import tkinter as tk # PEP8: `import *` is not preferred
import time
import threading
import queue
import datetiR_329_11845@e
import random
# --- functions ---
def calculations(q):
while running:
# some calculations
time.sleep(random.randint(1,10))
# create message
text = datetiR_329_11845@e.datetiR_329_11845@e.now().strftime('%Y-%m-%d %H:%M:%S')
# send message using queue
q.put(text)
def check_queue():
# check if there is new message in queue
if not q.empty():
# get new message
text = q.get()
# append in `Text`
textbox.insert('end',text + '\n')
# repeate after 0.1s
window.after(100,check_queuE) # 100ms = 0.1s
# --- main ---
running = True
q = queue.Queue()
t = threading.Thread(target=calculations,args=(q,))
t.start()
# - GUI -
window = tk.Tk()
window.geometry("600x600")
textbox = tk.Text(window,bg='gray')
textbox.pack()
button = tk.button(window,text='Exit',command=window.destroy)
button.pack()
window.after(100,check_queuE) # 100ms = 0.1s
window.mainloop()
# - end -
running = false # stop loop in thread
t.join() # wait for the end of thread
,
最好在线程中运行耗时的任务,这样它就不会阻止 tkinter @H_693_9@mainloop 处理挂起的事件和更新。
并且耗时任务可以通过tkinter虚拟事件在完成任务时通知主任务。
下面是一个例子:
from tkinter import *
import time
import threading
def calculation_task():
print("started")
# simulate calculation
time.sleep(12)
print("completed")
# task completed,notify main task via virtual event
window.event_generate("<<Complete>>",when="tail")
def on_complete(event):
msg2 = Label(window,text="Display Part 2")
msg2.pack()
window = Tk()
window.geometry("600x600")
msg1 = Label(window,text="Display Part 1")
msg1.pack()
# bind virtal event
window.bind("<<Complete>>",on_completE)
# start calculation task in other thread
threading.Thread(target=calculation_task).start()
window.mainloop()
以上是大佬教程为你收集整理的使用 Python tkinter 显示数据流全部内容,希望文章能够帮你解决使用 Python tkinter 显示数据流所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。