简体   繁体   English

Tkinter Listobox Selection反复调用/自动刷新功能

[英]Tkinter Listobox Selection calls a Function repeatedly/autorefresh

I am running a GUI in Python 3 with Tkinter implementation. 我正在使用Tkinter实现在Python 3中运行GUI。

I am populating a Listbox with some values that I query from a database and on select of a result from the Listbox, I "bind" a function. 我用从数据库中查询的一些值填充列表框,并从列表框中选择结果,然后“绑定”一个函数。

tagsList.bind('<<ListboxSelect>>', get_anchors)

This function/event widget, queries again another database that has saved the location of some PNG images and imports them to the GUI using: ImageTk.PhotoImage(Image.open(path)) , as PhotoImage. 此功能/事件窗口小部件再次查询保存了某些PNG图像位置的另一个数据库,并使用: ImageTk.PhotoImage(Image.open(path))作为PhotoImage将其导入到GUI。 It populates a grid with all these images loaded, as you may see in the code below: 如下面的代码所示,它将在网格中填充所有已加载的图像:

def get_anchors(event):
    lb = event.widget
    index = lb.curselection()[0]
    tag_name = lb.get(index)

    # Get the anchor list from the database row
    alist = []
    for row in conn.execute("SELECT anchor FROM \"" + tag_name + "\" ORDER BY anchor"):
        alist.append(row[0])

    # ===== Import PNG Images =====
    i = 1
    for anchor in alist:
        j = 1
        for key in measurements:
            path = conn.execute("SELECT \"" + key + "\" FROM \"" + tag_name + "\" WHERE anchor = \"" + anchor +"\"").fetchone()[0]
            photo = ImageTk.PhotoImage(Image.open(path))
            label = tkinter.Label(mainWindow, relief="sunken", image=photo)
            label.image = photo
            label.grid(row=i, column=j, sticky='nsew')
            j += 1
        i += 1

The Problem: The PNG images are changing over the time, they are basically replaced with new generated ones from another program. 问题: PNG图像随着时间的推移而变化,它们基本上被其他程序中新生成的图像所替代。 So, I was wondering if the is a way to autorefresh/load again the newly PNG files, without having to click/select again the same result from the Listbox, that I already have it selected. 因此,我想知道是否有一种方法可以自动刷新/重新加载新的PNG文件,而不必再次单击/选择列表框中已经选择的相同结果。

The PNG images are generated every 5 seconds, if that helps and they are basically replacing the existing ones(same file name, location etc.). PNG图像每5秒钟生成一次,如果有帮助的话,它们基本上是在替换现有的图像(文件名,位置等)。 I was hoping that there might be something like a constant autorefresh feature/method or at least a way to call this get_anchors function repeatedly once a specific result is selected from the Listbox. 我希望一旦从列表get_anchors选择了特定的结果,就可能会有类似恒定的自动刷新功能/方法,或者至少有一种方法可以反复调用此get_anchors函数。

I can't really find something that will enable this functionality and the only way to go right now, is by clicking again the option that refreshes the grid with the new PNGs by calling again the get_anchors . 我真的找不到能够启用此功能的东西,并且现在唯一get_anchors方法是,再次单击再次调用get_anchors来刷新带有新PNG的网格的选项。

Not entirely sure why it was not working before with the lb.after(5000, get_anchors, args=(event,)) , but it is working now in the format lb.after(5000, get_anchors, event) . 不能完全确定为什么lb.after(5000, get_anchors, args=(event,))不起作用,但现在它的工作格式为lb.after(5000, get_anchors, event)

I am basically calling a function from the main Tkinter code like this: 我基本上是从这样的主要Tkinter代码中调用一个函数:

tagsList.bind('<<ListboxSelect>>', get_anchors)

which is a listbox and once I select my choice, it calls the get_anchors function, which I bind to it. 这是一个列表框,一旦我选择了我的选择,它就会调用get_anchors函数,并将其绑定到该函数。 Then, all I wanted was to re-call the function every 6 seconds from within the same function get_anchors . 然后,我想要的是每6秒从同一函数get_anchors重新调用该函数。 The code looks like: 代码如下:

def get_anchors(event):
    # ===== Clear canvas and re-assign it on GUI =====
    canvas.delete("all")
    canvas.grid(row=1, column=1, sticky='nsew', rowspan=17, columnspan=5, padx=(10, 0))
    image_plots = tkinter.Frame(canvas, background='white')
    canvas.create_window((0,0), window=image_plots, anchor='nw')

    lb = event.widget
    index = lb.curselection()[0]
    tag_name = lb.get(index)

    # Get the anchor list from the database row
    alist = []
    for row in conn.execute("SELECT anchor FROM \"" + tag_name + "\" ORDER BY anchor"):
        alist.append(row[0])

    # ===== Import PNG Images =====
    i = 1
    for anchor in alist:
        j = 1
        for key in measurements:
            path = conn.execute("SELECT \"" + key + "\" FROM \"" + tag_name + "\" WHERE anchor = \"" + anchor +"\"").fetchone()[0]
            photo = ImageTk.PhotoImage(Image.open(path))
            label = tkinter.Label(image_plots, relief="sunken", image=photo)
            label.image = photo  # Keeping reference is needed
            label.grid(row=i, column=j, sticky='nsew')
            j += 1
        i += 1

    # Refresh PNGs every 6s; they are generated roughly every 5s
    mainWindow.after(6000, get_anchors, event)

This solution at the last line works for refreshing the Canvas and populating it with the new PNGs. 最后一行的此解决方案用于刷新Canvas并使用新的PNG填充它。 With the args=(event,) solution, it did not work. 使用args=(event,)解决方案时,它不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM