0

测试模板.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))

主要.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在代码中),当列表到达末尾时,它应该关闭窗口。

4

1 回答 1

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
于 2017-04-12T09:40:48.410 回答