PyQt 从 GUI 获取值 [英] PyQt get value from GUI

查看:40
本文介绍了PyQt 从 GUI 获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 QtDesigner 构建了一个用户界面,然后将 .ui 转换为 .py.用户界面具有不同的 comboBoxtextBox,一旦单击运行"按钮,我想从中读取值.运行一个函数,然后在计算完成后填充用户界面的其他文本框.但是,当我更改 comboBox 的值并单击按钮时,脚本仍会读取初始值.

I've built an User Interface using QtDesigner and then converted the .ui to .py. The User Interface has different comboBox and textBox from which I want to read the values once the Run button is clicked. Run a function and then populate other text boxes of the user interface once the calculations are completed. However when I change the value of the comboBox and click the button the script still reads the initial value.

我用一个带有两个项目和一个文本框的组合框做了一个简单的 GUI.我正在尝试读取组合框文本并根据所选项目设置文本框的文本.

I did a simple GUI with a comboBox with two items and a textBox. I'm trying to read the the comboBox text and based on the selected item set the text of the textBox.

这是我用来运行 GUI 并读取值的代码:

Here is the code I'm using to run the GUI and read the value:

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))

有什么想法吗?

谢谢

推荐答案

如果您在运行中实现的代码是我认为您正在滥用线程的代码,因为使用 currentTextChanged 发出信号正如我在下面展示的那样就足够了:

If the code that you implement in the run is the one that I think you are abusing the threads since with the currentTextChanged signal it would be enough as I show below:

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:

On the other hand if the real code is a time-consuming task then the use of the threads is adequate. If the task takes as reference the value of the QComboBox at the moment of pressing the button then it establishes that value as property of the thread, in your case you are creating a new GUI in another thread instead of using the existing 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()

这篇关于PyQt 从 GUI 获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆