简体   繁体   中英

PyQt5: clicked.connect not working with no errors

Put simply my binding of signals to slots does not seem to be working, and I'm not getting any errors. Printing a message doesn't work in the function either.

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

I cannot run your code, but main issue seems to be incorrect indentation. Only your clicked.connect calls should be in the constructor (above all other methods), the other methods outside. See following runnable template for how should your code be structured:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM