Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python函数切换显示的图像_Python_Image_Tkinter_Classification_Visualization - Fatal编程技术网

Python函数切换显示的图像

Python函数切换显示的图像,python,image,tkinter,classification,visualization,Python,Image,Tkinter,Classification,Visualization,我有一个运行在实时数据上的Python3脚本,它调用一个分类函数,该函数返回0或1。我想让窗口根据分类器返回的内容显示图像 以下是我的分类器脚本的简单版本: from random import randint import time def classifier(): time.sleep(4) return randint(0,1) while True: classification=classifier() 以下是我迄今为止为视觉效果设计的脚本: impor

我有一个运行在实时数据上的Python3脚本,它调用一个分类函数,该函数返回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,当您在GUI代码中使用其他代码,然后在其他代码中使用GUI代码时,会更容易,它被称为“GUI包装器”

GUI的问题是它必须运行
mainloop
,它一直工作(直到您关闭窗口),并且您不能运行其他循环。您可以使用
thread
同时运行其他循环,但
tkinter
不是线程安全的,在其他线程中更改小部件可能会产生问题

您可以使用
root.after
定期运行一些代码,然后两个循环就没有问题了,因为
mainloop
将为您运行它

import tkinter as tk
from PIL import Image,ImageTk
from random import randint, randrange
import time

# --- classes ---

class Application(tk.Frame):

    def __init__(self, master=None):
        super().__init__(master, bg="")
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        # create empty label
        self.arrow = tk.Label(self)
        self.arrow.pack()

    def update_image(self, img):
        # set image in label
        self.arrow.config(image=img)

        # because image is assigned to global list `imgs`
        # so this command may not be needed        
        self.arrow.image = img

# --- functions ---

def classifier():
    # return randint(0, len(imgs)-1) # with -1
    return randrange(0, len(imgs))   # without -1

def update_loop():
    # run your classifier from other file
    selected = classifier()

    # get image from list
    img = imgs[selected]

    # update image in window
    app.update_image(img)

    # run it again after 4000ms
    root.after(4000, update_loop)

# --- main ---

root = tk.Tk()

# list with images (create after creating `root`)
imgs = [
    ImageTk.PhotoImage(Image.open('left.jpg')),
    ImageTk.PhotoImage(Image.open('right.jpg')),
]    

# creacte app
app = Application(root)

# run it first time
update_loop()

# start "engine"
app.mainloop() 

这是我用的东西

import tkinter as tk
from PIL import Image,ImageTk
from threading import Thread
from queue import Queue
import time
from random import randint

q=Queue() #both threads can access the queue

def GUIthread(): #all GUI actions take place in this thread
    root = tk.Tk() 
    img1=ImageTk.PhotoImage(Image.open('left.jpg'))
    img2=ImageTk.PhotoImage(Image.open('right.jpg'))

    def updateImage(classification):
        if classification ==1:
            root.arrow.config(image=img2)
            root.arrow.image=img2
            root.arrow.pack()
        else:
            root.arrow.config(image=img1)
            root.arrow.image=img1
            root.arrow.pack()

    def checkQueue():
        classification=q.get()
        updateImage(classification)
        q.task_done()
        root.after(1000,checkQueue)

    root.arrow=tk.Label(image=img1)
    root.arrow.image=img1
    root.arrow.pack()          
    root.after(1, checkQueue)
    root.mainloop()


def classifier(): #mimics a thread that needs to be run continuously (collecting live data)
    while 1: 
        time.sleep(2)
        q.put(randint(0,1))


t=Thread(target=GUIthread)
t.start()
classifier()

假设您的图像位于标签中,只需使用
Label\u instance.config(image=new\u image)
更新标签即可使用新图像。如果你想要一个具体的答案,你必须给我们一个答案。我已经更新了这个问题,希望这能让它更清楚!谢谢你的帮助!不幸的是,在分类器脚本需要持续运行时,这对我来说不起作用。我用线程和队列工作,将在上面发布