【发布时间】:2018-06-29 04:49:26
【问题描述】:
我有一个在实时数据上运行的 python 3 脚本,该脚本调用一个分类函数,该函数返回 0 或 1。我希望窗口根据分类器返回的内容显示图像。
这是我的分类器脚本的一个更简单的版本:
from random import randint
import time
def classifier():
time.sleep(4)
return randint(0,1)
while True:
classification=classifier()
这是我目前为视觉效果设计的脚本:
import tkinter as tk
from PIL import Image,ImageTk
root = tk.Tk()
img1=ImageTk.PhotoImage(Image.open('left.jpg'))
img2=ImageTk.PhotoImage(Image.open('right.jpg'))
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master, bg="")
self.pack()
self.create_widgets()
def create_widgets(self):
self.arrow=tk.Label(self, image=img1)
self.arrow.image=img1 #must keep this otherwise will be discarded
self.arrow.pack()
self.pack()
def updateImg(img):
app.arrow.config(image=img)
app.arrow.image=img
app.arrow.pack()
app = Application(master=root)
app.mainloop()
我的问题是连接两个脚本,这样分类的值决定了实时显示的图像。理论上我希望能够从分类器脚本中调用 updateImg
【问题讨论】:
-
假设您的图像位于标签中,只需更新标签以使用带有
label_instance.config(image=new_image)的新图像。如果您想要一个具体的答案,您必须向我们展示minimal reproducible example。 -
我已经更新了这个问题,希望它更清楚!
标签: python image tkinter classification visualization