2017-04-12 3 views
0

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

質問:

ボタンをクリックするたびに、テキストがリスト内の次のエントリ(コード内でtext_list)に変更され、リストの終わりに達するとウィンドウが閉じられるはずです。

答えて

1

私は、次のようtext_listを定義します

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 
+0

かしこい:

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方法は次のようになります!私はリストをループしようとしていました。要素をポップすると正確な結果が得られます。ありがとう:) – x899