[英]Python ; Cant share variable and update Qt GUI between threads
The integer variable defined in thread 1 does not want to share to thread 2, nor does the UI want to update on an simple loop to update it. 线程1中定义的整数变量不希望与线程2共享,UI也不想在简单的循环上进行更新以对其进行更新。
Main.py 主程序
from ui import Ui_main_window
from PyQt4 import QtGui, QtCore
import sys
import subprocess
import commands
import threading
class MainWindow(QtGui.QMainWindow, Ui_main_window):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
def _dd_thread_run(_if, _of, _bs, _size):
_dd_subprocess_command_format = "dd if=%s bs=%s | pv -n --size %s | dd of=%s" % (_if, _bs, _size, _of)
_dd_subprocess_command = [_dd_subprocess_command_format]
_dd_progress = subprocess.Popen(_dd_subprocess_command, shell=True, stderr=subprocess.PIPE)
while _dd_progress.poll() is None:
out = _dd_progress.stderr.readline().replace("\n", "")
out_int = int (out)
print "[DEBUG] %r" % out_int
def _dd_do():
_dd_thread_run("/dev/urandom", "/dev/null", "100K", "100M")
def _ui_progress_set():
while True:
for i in range(0, 100):
ui2 = MainWindow()
ui2.progressBar.setValue(i) # < Not working, does nothing, no error
print out_int # < Throws error, "not defined"
app = QtGui.QApplication(sys.argv)
ui = MainWindow()
ui.show()
t1 = threading.Thread(target=_dd_do, args=[])
t1.start()
t2 = threading.Thread(target=_ui_progress_set, args=[])
t2.start()
sys.exit(app.exec_())
The program is pretty simple and straight forward. 该程序非常简单直接。 Have i missed something?
我错过了什么吗? How to handle a global variable to use between threads?
如何处理在线程之间使用的全局变量?
Upon starting the second thread, it immediately throws the error that out_int is not defined. 启动第二个线程后,它立即引发未定义out_int的错误。
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "untitled3.py", line 55, in _ui_progress_set
print out_int
NameError: global name 'out_int' is not defined
[DEBUG] 12
[DEBUG] 25
[DEBUG] 38
[DEBUG] 52
[DEBUG] 65
[DEBUG] 79
[DEBUG] 92
[DEBUG] 94
While it is, here: out_int = int (out)
虽然是,但在这里:
out_int = int (out)
But, as you can see, the integer is formatted correctly and printed to the console. 但是,如您所见,该整数已正确格式化并打印到控制台。
What you do is equivalent to: 您所做的等同于:
#!/usr/bin/python
def first() :
out_int = 1
def second() :
print out_int
first()
second()
Which results in : 结果是:
Traceback (most recent call last):
File "./test.py", line 12, in <module>
second()
File "./test.py", line 8, in second
print out_int
NameError: global name 'out_int' is not defined
So you have to define your out_int
on the global scope and declare in the modifying function that you want to access that global variable. 因此,您必须在全局范围内定义
out_int
,并在修改函数中声明要访问该全局变量。
The solution looks like: 解决方案如下:
#!/usr/bin/python
out_int = 0
def first() :
global out_int
out_int = 1
def second() :
print out_int
first()
second()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.