【问题标题】:Change text in a window on button click单击按钮更改窗口中的文本
【发布时间】:2017-09-07 22:33:18
【问题描述】:

testTemplate.py:

from PySide import QtCore, QtGui

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(153, 130)
        self.testlabel = QtGui.QLabel(Dialog)
        self.testlabel.setGeometry(QtCore.QRect(50, 40, 46, 13))
        self.testlabel.setObjectName("testlabel")
        self.NextButton = QtGui.QPushButton(Dialog)
        self.NextButton.setGeometry(QtCore.QRect(40, 80, 75, 23))
        self.NextButton.setObjectName("NextButton")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
        self.testlabel.setText(QtGui.QApplication.translate("Dialog", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))
        self.NextButton.setText(QtGui.QApplication.translate("Dialog", "Next", None, QtGui.QApplication.UnicodeUTF8))

main.py:

from PySide.QtCore import *
from PySide.QtGui import *
import sys
import testTemplate

class MainDialog( QDialog, testTemplate.Ui_Dialog ):
    def __init__(self, parent=None):
        super(MainDialog, self).__init__(parent)
        self.setupUi(self)
        self.connect(self.NextButton, SIGNAL("clicked()"), self.changetext)
        text_list = ['abc','xyz','bvc']

    def changetext(self):
        print "print"

app = QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()

输出窗口:

问题:

每当我单击Next 按钮时,我希望文本更改为列表中的下一个条目(代码中为text_list),当列表到达末尾时,它应该关闭窗口。

【问题讨论】:

    标签: python python-2.7 user-interface pyside pyside2


    【解决方案1】:

    我将text_list 定义如下:

    self.text_list = ['abc','xyz','bvc']
    self.text_list.reverse()  # in case you want to display items in the same order as is given the list
    

    那么,changetext 方法可能如下所示:

    def changetext(self):
        if self.text_list:  # test whether list is nonempty
            self.testlabel.setText(self.text_list.pop())
        else:
            self.close()    # close the application
    

    【讨论】:

    • 聪明!我试图遍历列表。弹出元素将给出确切的结果。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多