0

コンボ ボックス、プッシュ (トグル) ボタン、テキスト編集コントロールの 3 つのコントロールを持つ単純なウィンドウがあるとします。それを盛り上げるために、(トグルが押されている間) スレッドで実行するアルゴリズムが必要でしたが、私が抱えている主な問題の解決策はそれに影響されないと思います

トグル ボタンが押されたときに、現在の ComboBox 値を読み取り、TextEdit の値を更新したいと考えています。

これは私が試して失敗したアプローチです。ばかげた間違いかもしれませんし、デザインをまったく変更する必要があるかもしれません (QML はこれを少し簡単にするのに役立ちますか?):

#!/usr/bin/env python
import sys
import threading

try:
    from PySide import QtGui
    from PySide import QtCore
except:
    from PyQt4.QtCore import pyqtSlot as Slot
    from PyQt4 import QtGui
    from PyQt4 import QtCore


class MyUI(QtGui.QWidget):

    def __init__(self):
        super(MyUI, self).__init__()
        self.initUI()

    def initUI(self):

        someValues = [1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200]

        self.setGeometry(100, 300, 640, 450) #window's geometry

        lblValueChoice = QtGui.QLabel(self)
        lblValueChoice.setGeometry(10, 10, (self.width() - 20), 27)
        lblValueChoice.setText("Select your value:")

        cmbValueChoice = QtGui.QComboBox(self)
        cmbValueChoice.setObjectName("valueChoice")
        cmbValueChoice.setGeometry(10, (lblValueChoice.y() + lblValueChoice.height() + 5), (self.width() - 20), 27)

        for item in someValues: cmbSerialPorts.addItem(item)
        cmbSerialPorts.setCurrentIndex(len(someValues)-1)

        pbStartReading = QtGui.QPushButton("Start doing the magic!", self)
        pbStartReading.setGeometry(10, (cmbValueChoice.y() + cmbValueChoice.height() + 10), (self.width() - 20), 27)
        pbStartReading.setCheckable(True)
        pbStartReading.clicked[bool].connect(lambda: self.startReading(bool, str(cmbValueChoice.currentText())))

        textEdit = QtGui.QTextEdit(self)
        textEdit.setObjectName("textEdit")
        textEdit.setGeometry(10, (pbStartReading.y() + pbStartReading.height() + 10), (self.width() - 20), (self.height() - (pbStartReading.y() + pbStartReading.height() + 10) - 10) )

        textEdit.append(add_env())

        self.setWindowTitle(u'MyMiserableUIFailure')

        self.show()

    def startReading(self, bool, myvalue):

        threading.Thread(target=self.readingLoop, args=(bool, myvalue, )).start()

    def readingLoop(self, bool, myvalue):

        while bool:
            # the error happens in the line below, when I
            # try to reference the textEdit control
            # -------------------------------------------
            textEdit.append("this actually works!\n")


def main():
    app = QtGui.QApplication(sys.argv)

    theui = MyUI()

    sys.exit(app.exec_())

def add_env():
    newLine = QtCore.QString("\n")
    env_info = "The system is:" + newLine
    env_info += newLine + "System/OS name:            " + str(platform.system())
    env_info += newLine + "System release:          " + str(platform.release())
    env_info += newLine + "System release version:  " + str(platform.version())
    env_info += newLine + "Machine type:              " + str(platform.machine())
    env_info += newLine + "Platform:                  " + str(platform.platform(aliased=0, terse=1))
    env_info += newLine + "Processor:                 " + str(platform.processor())
    env_info += newLine + "Network name:              " + str(platform.node())
    env_info += newLine + "Python ver(maj,min,patch): " + str(platform.python_version_tuple())
    env_info += newLine + "Python build:              " + str(platform.python_build())
    env_info += newLine + "Python implementation:     " + str(platform.python_implementation())
    env_info += newLine
    env_info += newLine + "***************************"
    return env_info

if __name__ == '__main__':
    main()
4

1 に答える 1

0

別のスレッドから ui 要素を直接参照することはできません。これにはシグナルを使用する必要があります。コードで変更しました。以下を参照してください。私はそれをテストしませんでしたが、これはこれが行われるべき方法です。

#!/usr/bin/env python
import sys
import threading

try:
    from PySide import QtGui
    from PySide import QtCore
    from PySide.QtCore import Signal
except:
    from PyQt4.QtCore import pyqtSignal as Signal
    from PyQt4 import QtGui
    from PyQt4 import QtCore


class MyUI(QtGui.QWidget):
# Add Signal
    text_edit_evt = Signal(str)

    def __init__(self):
        super(MyUI, self).__init__()
        self.initUI()

# Connect the signal
        self.text_edit_evt.connect(self.textEdit.append)

    def initUI(self):

        someValues = [1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200]

        self.setGeometry(100, 300, 640, 450) #window's geometry

        lblValueChoice = QtGui.QLabel(self)
        lblValueChoice.setGeometry(10, 10, (self.width() - 20), 27)
        lblValueChoice.setText("Select your value:")

        cmbValueChoice = QtGui.QComboBox(self)
        cmbValueChoice.setObjectName("valueChoice")
        cmbValueChoice.setGeometry(10, (lblValueChoice.y() + lblValueChoice.height() + 5), (self.width() - 20), 27)

        for item in someValues: cmbSerialPorts.addItem(item)
        cmbSerialPorts.setCurrentIndex(len(someValues)-1)

        pbStartReading = QtGui.QPushButton("Start doing the magic!", self)
        pbStartReading.setGeometry(10, (cmbValueChoice.y() + cmbValueChoice.height() + 10), (self.width() - 20), 27)
        pbStartReading.setCheckable(True)
        pbStartReading.clicked[bool].connect(lambda: self.startReading(bool, str(cmbValueChoice.currentText())))

# Use self.textEdit so it can be referenced from other functions
        self.textEdit = QtGui.QTextEdit(self)
        self.textEdit.setObjectName("textEdit")
        self.textEdit.setGeometry(10, (pbStartReading.y() + pbStartReading.height() + 10), (self.width() - 20), (self.height() - (pbStartReading.y() + pbStartReading.height() + 10) - 10) )

        self.textEdit.append(add_env())

        self.setWindowTitle(u'MyMiserableUIFailure')

        self.show()

    def startReading(self, bool, myvalue):

        threading.Thread(target=self.readingLoop, args=(bool, myvalue, )).start()

    def readingLoop(self, bool, myvalue):

        while bool:
            # the error happens in the line below, when I
            # try to reference the textEdit control
            # -------------------------------------------
# Emit the Signal we created
            self.text_edit_evt.emit("this actually works!\n")


def main():
    app = QtGui.QApplication(sys.argv)

    theui = MyUI()

    sys.exit(app.exec_())

def add_env():
    newLine = QtCore.QString("\n")
    env_info = "The system is:" + newLine
    env_info += newLine + "System/OS name:            " + str(platform.system())
    env_info += newLine + "System release:          " + str(platform.release())
    env_info += newLine + "System release version:  " + str(platform.version())
    env_info += newLine + "Machine type:              " + str(platform.machine())
    env_info += newLine + "Platform:                  " + str(platform.platform(aliased=0, terse=1))
    env_info += newLine + "Processor:                 " + str(platform.processor())
    env_info += newLine + "Network name:              " + str(platform.node())
    env_info += newLine + "Python ver(maj,min,patch): " + str(platform.python_version_tuple())
    env_info += newLine + "Python build:              " + str(platform.python_build())
    env_info += newLine + "Python implementation:     " + str(platform.python_implementation())
    env_info += newLine
    env_info += newLine + "***************************"
    return env_info

if __name__ == '__main__':
    main()
于 2013-05-12T12:30:31.910 に答える