简单地说,我的信号绑定到插槽似乎不起作用,我没有收到任何错误。打印消息也不在该功能中工作。
# -*- 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_())
答案 0 :(得分:2)
我无法运行您的代码,但主要问题似乎是不正确的缩进。只有你的clicked.connect
调用应该在构造函数中(在所有其他方法之上),其他方法在外面。请参阅以下runnable模板,了解如何构建代码:
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_())