我使用构建了一个用户界面QtDesigner
然后转换.ui
to .py
。用户界面有不同comboBox
and textBox
单击“运行”按钮后我想从中读取值。运行函数,然后在计算完成后填充用户界面的其他文本框。但是当我改变的值comboBox
然后单击按钮,脚本仍然读取初始值。
我做了一个简单的 GUI,其中包含一个包含两个项目和一个文本框的组合框。我正在尝试读取组合框文本并根据所选项目设置文本框的文本。
这是我用来运行的代码GUI
并读取值:
from PyQt4 import QtGui
from pyQt4 import QtCore
import sys
import GUI
class MyThread(QtCore.QThread):
updated = QtCore.pyqtSignal(str)
def run(self):
self.gui = Window()
name = self.gui.gui_Name.currentText()
print (name)
if name == 'Cristina':
country = 'Italy'
else:
country = 'Other'
self.updated.emit(str(1))
class Window(QtGui.QMainWindow, GUI.Home):
def __init__(self,parent = None):
super(Window,self).__init__(parent)
self.setupUi(self)
self._thread = MyThread(self)
self._thread.updated.connect(self.updateText)
self.update()
self.
self.pushButton.clicked.connect(self._thread.start)
def updateText(self,text):
self.Country.setText(str(country))
有什么想法吗?
Thanks
如果您在运行中实现的代码是我认为您正在滥用线程的代码,因为currentTextChanged
信号就足够了,如下所示:
class Window(QtGui.QMainWindow, GUI.Home):
def __init__(self,parent = None):
super(Window,self).__init__(parent)
self.setupUi(self)
self.gui_Name.currentTextChanged.connect(self.onCurrentTextChanged)
def onCurrentTextChanged(self, text):
if if name == 'Cristina':
country = 'Italy'
else:
country = 'Other'
self.Country.setText(str(country))
另一方面,如果实际代码是一项耗时的任务,那么使用线程就足够了。如果任务以QComboBox
在按下按钮的那一刻,它会将该值建立为线程的属性,在您的情况下,您正在另一个线程中创建一个新的 GUI,而不是使用现有的 GUI:
class MyThread(QtCore.QThread):
updated = QtCore.pyqtSignal(str)
def run(self):
name = self.currentText
print(name)
if name == 'Cristina':
country = 'Italy'
else:
country = 'Other'
self.updated.emit(country)
class Window(QtGui.QMainWindow, GUI.Home):
def __init__(self,parent = None):
super(Window,self).__init__(parent)
self.setupUi(self)
self._thread = MyThread(self)
self._thread.updated.connect(self.Country.setText)
self.pushButton.clicked.connect(self.start_thread)
def start_thread(self):
self._thread.currentText = self.gui_Name.currentText()
self._thread.start()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)