【问题标题】:PyQt5: clicked.connect not working with no errorsPyQt5:clicked.connect 无法正常工作且没有错误
【发布时间】:2014-09-10 06:44:48
【问题描述】:

简单地说,我将信号绑定到插槽似乎不起作用,而且我没有收到任何错误。打印消息在该功能中也不起作用。

# -*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, uic
from PyQt5.QtWidgets import *
from PyQt5.QtGui import (QBrush, QColor, QFont, QLinearGradient, QPainter, QPainterPath, QPalette, QPen)
from APM_ui import Ui_Window
import random, sys

print('... APM Loading ...')

uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
PasswordFont = QFont('Lato', 11, QFont.Bold, True)

class Window(Ui_Window):
    def __init__(self, parent = None, name = None, fl = 0):
        Ui_Window.__init__(self, parent, name, fl)


        def createDefaultClicked(self):
            default = []
            _translate = QtCore.QCoreApplication.translate
            done = 0
            while True:
                if done!= 12:
                    random.shuffle(lowercase)
                    random.shuffle(uppercase)
                    decision = random.randint(0, 2)
                    if decision == 0:
                        default.append(random.randint(0, 9))
                        done += 1
                        continue
                    if decision == 1:
                        default.append(lowercase[0])
                        done += 1
                        continue
                    if decision == 2:
                        default.append(uppercase[0])
                        done += 1
                        continue
                if done == 12:
                    break
            self.defaultPWDisp.setFont(PasswordFont)
            defaultPassword = '{}{}{}{}{}{}'.format(*default)
            self.defaultPWDisp.setText(_translate('Window', '<html><head/><body><p align=\'center\'>%s is your new password!</p></body></html>' % defaultPassword))


        def createSmallClicked(self):
            small = []
            _translate = QtCore.QCoreApplication.translate
            done = 0
            while True:
                if done!= 6:
                    random.shuffle(lowercase)
                    random.shuffle(uppercase)
                    decision = random.randint(0, 2)
                    if decision == 0:
                        small.append(random.randint(0, 9))
                        done += 1
                        continue
                    if decision == 1:
                        small.append(lowercase[0])
                        done += 1
                        continue
                    if decision == 2:
                        small.append(uppercase[0])
                        done += 1
                        continue
                if done == 6:
                    break
            self.smallPWDisp.setFont(PasswordFont)
            smallPassword = '{}{}{}{}{}{}'.format(*small)
            self.smallPWDisp.setText(_translate('Window', '<html><head/><body><p align=\'center\'>%s is your new password!</p></body></html>' % smallPassword))

        def createTinyClicked(self):
            tiny = []
            _translate = QtCore.QCoreApplication.translate
            done = 0
            while True:
                if done!= 6:
                    random.shuffle(lowercase)
                    random.shuffle(uppercase)
                    decision = random.randint(0, 2)
                    if decision == 0:
                        tiny.append(random.randint(0, 9))
                        done += 1
                        continue
                    if decision == 1:
                        tiny.append(lowercase[0])
                        done += 1
                        continue
                    if decision == 2:
                        tiny.append(uppercase[0])
                        done += 1
                        continue
                if done == 6:
                    break
            self.tinyPWDisp.setFont(PasswordFont)
            tinyPassword = '{}{}{}{}{}{}'.format(*tiny)
            self.tinyPWDisp.setText(_translate('Window', '<html><head/><body><p align=\'center\'>%s is your new password!</p></body></html>' % tinyPassword))

        def createMidgetClicked(self):
            defaultPassword = []
            done = 0
            while True:
                if done!= 4:
                    random.shuffle(lowercase)
                    random.shuffle(uppercase)
                    decision = random.randint(0, 2)
                    if decision == 0:
                        defaultPassword.append(random.randint(0, 9))
                        done += 1
                        continue
                    if decision == 1:
                        defaultPassword.append(lowercase[0])
                        done += 1
                        continue
                    if decision == 2:
                        defaultPassword.append(uppercase[0])
                        done += 1
                        continue
                if done == 4:
                    break
            self.midgetPWDisp.setFont(PasswordFont)
            midgetPassword = '{}{}{}{}{}{}'.format(*midget)
            self.midgetPWDisp.setText(_translate('Window', '<html><head/><body><p align=\'center\'>%s is your new password!</p></body></html>' % midgetPassword))

# Look here! :P
    self.createDefault.clicked.connect(self.createDefaultClicked)   
    self.createSmall.clicked.connect(self.createSmallClicked)
    self.createTiny.clicked.connect(self.createTinyClicked)
    self.createMidget.clicked.connect(self.createMidgetClicked)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = QMainWindow()
    ui = Ui_Window()
    ui.setupUi(window)
    window.show()
    sys.exit(app.exec_())

【问题讨论】:

  • 通常最好包含代码,因为将来可能无法使用外部服务。我已包含您的代码并更改了描述以更好地适应似乎存在的问题。如果您不喜欢我的更改,可以还原。
  • 不使用APM ui能否重现问题?
  • 您的代码似乎缩进不正确。只有你的 clicked.connect 调用应该在构造函数中(高于所有其他方法),其他方法在外面。
  • 我已经更正了你的缩进,现在试试代码
  • 我没有看到您的 class Window(Ui_Window) 已在您的代码中创建对象。

标签: python python-3.x pyqt pyqt5


【解决方案1】:

我无法运行您的代码,但主要问题似乎是不正确的缩进。只有您的 clicked.connect 调用应该在构造函数中(高于所有其他方法),其他方法在外面。请参阅以下可运行模板以了解您的代码应如何构建:

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Window(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

        self.button1 = QPushButton("Test 1", self)
        self.button2 = QPushButton("Test 2", self)
        self.button3 = QPushButton("Test 3", self)

        self.layout = QHBoxLayout()
        self.layout.addWidget(self.button1)
        self.layout.addWidget(self.button2)
        self.layout.addWidget(self.button3)

        self.setLayout(self.layout)
        self.show()

        self.button1.clicked.connect(self.on_button1)
        self.button2.clicked.connect(self.on_button2)
        self.button3.clicked.connect(self.on_button3)

    @pyqtSlot()
    def on_button1(self):
        print("Button #1")

    @pyqtSlot()
    def on_button2(self):
        print("Button #2")

    @pyqtSlot()
    def on_button3(self):
        print("Button #3")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Window()
    sys.exit(app.exec_())

【讨论】:

  • 我的代码对你有用吗?如果是这样,我建议仅使用打印语句(类似于此)单独创建 GUI,并且仅当它按预期工作时才开始添加功能。您发布的代码很难调试,因为我们无法运行它,因为我们缺少 APM_ui 模块。或者,您可以创建一个MCVE。当您需要 GUI 帮助时,创建一个演示问题的最小可运行示例是关键。
  • 您的代码确实有效,我会尽快在原帖中添加 APM_ui
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-17
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多