在按钮上单击

时间:2017-04-12 08:42:42

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

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

输出窗口:

Output

问题:

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

1 个答案:

答案 0 :(得分: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