如何使用队列在python3中使用tkinter线程?

克里斯

我正在尝试在 tkinter 中使用队列在 tkinter 中定义线程,特别是在 python3

我在 python2 中有类似的代码使用类似的方法没有队列工作得很好,但在 python3 中,我读到的 tkinter 不允许使用 gui 进行多线程。我发现了一些使用队列进程的例子。他们概述了我想创建一个队列对象,一个可以访问该队列的新线程并检查主线程中的输入

#!/usr/bin/python3

from tkinter import *
import time
import threading
import queue
import subprocess

def temp_sensor(queue_list):
    warning = 0
    while True:
        var = "cat /sys/class/thermal/thermal_zone*/temp"
        temp_control = subprocess.check_output([var], shell=True)
        temp_length = len(temp_control)
        temp_control = temp_control[35:]
        temp_control = temp_control[:-4]
        temp_control = int(temp_control)
        degree_sign= u'\N{DEGREE SIGN}'
        displayed_temp = "Tempature: " + str(temp_control) + degree_sign + "C"

        if temp_control > 79:
            warning = warning + 1
            if warning == 3:
                print ("Warning Core Tempature HOT!")
                warning = 0

        if temp_control > 90:
                time.sleep(3)
                print ("Warning EXTREMLY to HOT!!!")

        queue_list.put(displayed_temp)            
        time.sleep(1)

class Gui(object):
    def __init__(self, queue_list):
        self.queue_list = queue_list
        self.root = Tk()
        self.root.geometry("485x100+750+475")
        main_tempature_status = StringVar(self.root)

        Ts = Entry(self.root, textvariable=main_tempature_status)
        Ts.pack()
        Ts.place(x=331, y=70, width=160, height=25)
        Ts.config(state=DISABLED, disabledforeground="Black")

        self.root.after(1000, self.read_queue)

    def read_queue(self):
        try:
            temp = self.queue.get_nowait()
            self.main_tempature_status.set(temp)
        except queue_list.Empty:
            pass

        self.root.after(1000, self.read_queue)

if __name__ == "__main__":
    queue_list = queue.Queue()

    gui = Gui(queue_list)
    t1 = threading.Thread(target=temp_sensor, args=(queue_list,))
    t1.start()

    gui.root.mainloop()

我想要的结果是运行其中一些定义来执行各种任务并使用 python3 在 tkinter 条目中显示它们的变量。当我运行我的代码时,它给了我队列中的变量,但它不会发布到 GUI。请原谅我比pythonic代码少。

卡迈勒

将您的Gui课程更改为:

class Gui(object):
    def __init__(self, queue_list):
        self.queue_list = queue_list
        self.root = Tk()
        self.root.geometry("485x100+750+475")
        self.main_tempature_status = StringVar(self.root)

        self.Ts = Entry(self.root, textvariable=self.main_tempature_status)
        self.Ts.pack()
        self.Ts.place(x=331, y=70, width=160, height=25)
        self.Ts.config(state=DISABLED, disabledforeground="Black")

        self.root.after(1000, self.read_queue)

    def read_queue(self):
        try:
            temp = self.queue_list.get_nowait()
            self.Ts.config(state=NORMAL)
            self.main_tempature_status.set(temp)
            self.Ts.config(state=DISABLED)
        except queue.Empty:
            pass

        self.root.after(1000, self.read_queue)

说明

  1. 变量main_temperature_status在函数中read_queue用作类变量,但未定义为类变量。
  2. Entry如果始终禁用小部件,则无法显示其值的变化,因此在read_queue.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在python3中使用tkinter的子帧

如何在Python中使用固定数量的线程处理队列中的多个作业

Tkinter 和 Python3 中的线程

如何在python3中使用Asterisk AGI?

如何在python中使用tkinter显示多线程视频?

如何在Python中使用Ctrl + C中止从队列中提取项目的线程

如何在线程中使用队列时为main中的特定变量创建锁

如何使用for循环在python3中使用不同的预处理函数?

如何在python3中使用python2 input()函数?

如何通过在python3中使用re模块匹配'aaabbbccc'中的'abbbc'?

如何在 Python3 中使用 *args

如何在 Python3 中使用 /n?

如何在 Python3 中使用 Mindee API?

如何在Python3中的MatLab的imshow()中使用'xdata'

Pandas:如何在python3中的混合类型多索引中使用切片?

如何在Python3中使用beautifulsoup查找配對內容

如何在python3中使用selenium在javascrip站点中选择下拉列表?

如何在python3中使用AST递归简化数学表达式?

如何在python3中使用pyclbr搜索__main__模块?

如何在python3中使用psycopg2从postgres获取数据,包括列名

如何在带有if __name __ ='__ main__'块的Python3中使用相对导入?

如何在python3中使用正则表达式获取值

如何在python3中使用GtkSource.FileLoader

如何在python3中使用数组join()方法添加数字?

如何在python3中使用子进程编写命令

如何在Python3中使用App Engine安排Cloud Datastore导出

如何在python3中使用ctypes导入ostringstream?

如何在Python3中使用opencv从文件缓冲区读取文件

当管道到ffmpeg时如何在python3中使用stdout.write?