【发布时间】: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