PySide,signal和slots [NameError:未定义全局名称'methodName']

时间:2013-03-10 01:15:01

标签: python pyside signals-slots

我是Python编程的新手,因为这个问题,我不能继续前进。

我的代码在上面:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
spike 1
9.03.2012
Kevin
"""

import sys

from PySide.QtCore import *
from PySide.QtGui import *

class GuiRenamator(QWidget):

    def __init__(self): # constructeur

        super(GuiRenamator, self).__init__() # je ne sais pas ce que c'est
        self.contenuGui()

    def dialogTest(self):
        dial_1 = QDialog()
        dial_1.setWindowTitle("Voici ce qui est écrit")
        text_dial_1 = QTextEdit()

        vblay_dial = QVBoxLayout()
        vblay_dial.addWidget(text_dial_1)

        self.setLayout(vblay_dial)

        # a bit of code
        if contenuGui.chk_ancienNom.isChecked():
            text_dial_1.append("- ajouter ancien nom")

        if contenuGui.chk_increment.isChecked():
            text_dial_1.append("- ajouter incrément")

        if contenuGui.chk_dateCreation.isChecked():
            text_dial_1.append("- ajouter date de création")

        dial_1.show()

    def contenuGui(self):

        lbl_nomDossier = QLabel("Nom du dossier: ")
        txt_nomDossier = QTextEdit()
        btn_selecDossier = QPushButton("+")
        chk_ancienNom = QCheckBox()
        chk_increment = QCheckBox()
        chk_dateCreation = QCheckBox()
        btn_ok = QPushButton("OK")
        **btn_ok.clicked.connect(dialogTest)**
        lbl_log = QLabel("Logs")
        txt_log = QTextEdit()

        # Layouts
        hblay_1 = QHBoxLayout()
        hblay_1.addWidget(btn_selecDossier)
        hblay_1.addWidget(lbl_nomDossier)
        hblay_1.addWidget(txt_nomDossier)

        flay_2 = QFormLayout()
        flay_2.addRow("Ancien nom", chk_ancienNom)
        flay_2.addRow("Incrément", chk_increment)
        flay_2.addRow("Date de création", chk_dateCreation)

        vblay_main = QVBoxLayout()
        vblay_main.addLayout(hblay_1)
        vblay_main.addLayout(flay_2)
        vblay_main.addWidget(btn_ok)
        vblay_main.addWidget(lbl_log)
        vblay_main.addWidget(txt_log)

        self.setLayout(vblay_main)

        self.setGeometry(300, 300, 300, 150)
        self.setWindowTitle("Ranamator")
        self.show()


def main():

    app = QApplication(sys.argv)
    gui = GuiRenamator()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

运行脚本时,会显示以下消息:

Traceback (most recent call last):
  File "./layout_spike_1.py", line 90, in <module>
    main()
  File "./layout_spike_1.py", line 86, in main
    gui = GuiRenamator()
  File "./layout_spike_1.py", line 20, in __init__
    self.contenuGui()
  File "./layout_spike_1.py", line 54, in contenuGui
    btn_ok.clicked.connect(dialogTest)
NameError: global name 'dialogTest' is not defined

当我评论该行时:btn_ok.clicked.connect(dialogTest) 它工作正常,但我无法将QPushButton连接到dialogTest方法。

我提前感谢你。

干杯,

凯文

1 个答案:

答案 0 :(得分:0)

btn_ok.clicked.connect(self.dialogTest)

替换该行