PyQt
和Python一般而言是全新的。我无法将点击事件应用于PyQt5
中的按钮。我正在尝试为所有按钮添加功能,尽管似乎我缺少一些初始设置以使其能够正常工作。浏览文档...
pushButton.clicked.connect("Do some action")
..应该启用目标按钮上的点击功能。
在connect
通话之后,我目前没有pushbutton.connect
选项可用。
我以为我在课堂上缺少某些设置。
from PyQt5 import QtCore, QtGui, QtWidgets
import cheekycheeky
import datetime
import ntplib
import time
class ContLCDClock(QtWidgets.QWidget,):
def __init__(self, parent = None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updateLCD1)
self.timer.start(1000)
# self.timer2 = QtCore.QTimer()
# self.timer2.timeout.connect(self.updateLCD2)
# self.timer2.start(4000)
# self.time_format = "%H:%M:%S"
# self.client = ntplib.NTPClient()
# self.response = self.client.request('pool.ntp.org')
# self.clock = time.strftime(self.time_format, time.localtime(self.response.tx_time))
def updateLCD1(self):
self.currentTime = QtCore.QTime.currentTime()
self.strCurrentTime = self.currentTime.toString('hh:mm:ss')
self.ui.lcdNumber.display(self.strCurrentTime)
# def updateLCD2(self):
# self.time_format = "%H:%M:%S"
# self.client = ntplib.NTPClient()
# self.response = self.client.request('pool.ntp.org')
# self.clock = time.strftime(self.time_format, time.localtime(self.response.tx_time))
# self.ui.lcdNumber_2.display(self.clock)
class Ui_Form(object) :
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(335, 157)
self.pushButton_7 = QtWidgets.QPushButton(Form)
self.pushButton_7.setGeometry(QtCore.QRect(50, 120, 31, 23))
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_7.clicked.connect(print('I am working')) # This does not work
self.pushButton_9 = QtWidgets.QPushButton(Form)
self.pushButton_9.setGeometry(QtCore.QRect(90, 120, 31, 23))
self.pushButton_9.setObjectName("pushButton_9")
self.pushButton_6 = QtWidgets.QPushButton(Form)
self.pushButton_6.setGeometry(QtCore.QRect(210, 120, 31, 23))
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(290, 120, 31, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(9, 16, 80, 19))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.pushButton_8 = QtWidgets.QPushButton(Form)
self.pushButton_8.setGeometry(QtCore.QRect(9, 120, 31, 23))
self.pushButton_8.setObjectName("pushButton_8")
self.pushButton_4 = QtWidgets.QPushButton(Form)
self.pushButton_4.setGeometry(QtCore.QRect(130, 120, 71, 23))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(Form)
self.pushButton_5.setGeometry(QtCore.QRect(250, 120, 31, 23))
self.pushButton_5.setObjectName("pushButton_5")
self.lcdNumber_2 = QtWidgets.QLCDNumber(Form)
self.lcdNumber_2.setGeometry(QtCore.QRect(145, 9, 171, 31))
self.lcdNumber_2.setObjectName("lcdNumber_2")
self.lcdNumber_2.setDigitCount(11)
self.lcdNumber = QtWidgets.QLCDNumber(Form)
self.lcdNumber.setGeometry(QtCore.QRect(145, 59, 171, 31))
self.lcdNumber.setObjectName("lcdNumber")
self.lcdNumber.setDigitCount(8)
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(9, 67, 105, 19))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Cheeky Timer"))
self.pushButton_7.setText(_translate("Form", "-.5"))
self.pushButton_9.setText(_translate("Form", "-.1"))
self.pushButton_6.setText(_translate("Form", "+.1"))
self.pushButton_3.setText(_translate("Form", "+1"))
self.label.setText(_translate("Form", "NTP Time:"))
self.pushButton_8.setText(_translate("Form", "-1"))
self.pushButton_4.setText(_translate("Form", "Reset Time"))
self.pushButton_5.setText(_translate("Form", "+.5"))
self.label_2.setText(_translate("Form", "Current Time:"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
c = ContLCDClock()
c.show()
sys.exit(app.exec_())
在此先感谢您的帮助,希望我已经足够清楚了。
答案 0 :(得分:0)
我想可能是因为您的输入不正确,例如:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 button - pythonspot.com'
self.left = 10
self.top = 10
self.width = 320
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
button = QPushButton('PyQt5 button', self)
button.setToolTip('This is an example button')
button.move(100,70)
button.clicked.connect(self.on_click)
self.show()
@pyqtSlot()
def on_click(self):
print('PyQt5 button click')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
此处传递了一个函数self.on_click
。在您的示例中,您传递了print('I am working')
,该返回了None
。您可以修复此问题,例如:
from PyQt5 import QtCore, QtGui, QtWidgets
import cheekycheeky
import datetime
import ntplib
import time
class ContLCDClock(QtWidgets.QWidget,):
def __init__(self, parent = None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updateLCD1)
self.timer.start(1000)
# self.timer2 = QtCore.QTimer()
# self.timer2.timeout.connect(self.updateLCD2)
# self.timer2.start(4000)
# self.time_format = "%H:%M:%S"
# self.client = ntplib.NTPClient()
# self.response = self.client.request('pool.ntp.org')
# self.clock = time.strftime(self.time_format, time.localtime(self.response.tx_time))
def updateLCD1(self):
self.currentTime = QtCore.QTime.currentTime()
self.strCurrentTime = self.currentTime.toString('hh:mm:ss')
self.ui.lcdNumber.display(self.strCurrentTime)
# def updateLCD2(self):
# self.time_format = "%H:%M:%S"
# self.client = ntplib.NTPClient()
# self.response = self.client.request('pool.ntp.org')
# self.clock = time.strftime(self.time_format, time.localtime(self.response.tx_time))
# self.ui.lcdNumber_2.display(self.clock)
class Ui_Form(object) :
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(335, 157)
self.pushButton_7 = QtWidgets.QPushButton(Form)
self.pushButton_7.setGeometry(QtCore.QRect(50, 120, 31, 23))
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_7.clicked.connect(self.i_am_working)
self.pushButton_9 = QtWidgets.QPushButton(Form)
self.pushButton_9.setGeometry(QtCore.QRect(90, 120, 31, 23))
self.pushButton_9.setObjectName("pushButton_9")
self.pushButton_6 = QtWidgets.QPushButton(Form)
self.pushButton_6.setGeometry(QtCore.QRect(210, 120, 31, 23))
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(290, 120, 31, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(9, 16, 80, 19))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.pushButton_8 = QtWidgets.QPushButton(Form)
self.pushButton_8.setGeometry(QtCore.QRect(9, 120, 31, 23))
self.pushButton_8.setObjectName("pushButton_8")
self.pushButton_4 = QtWidgets.QPushButton(Form)
self.pushButton_4.setGeometry(QtCore.QRect(130, 120, 71, 23))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(Form)
self.pushButton_5.setGeometry(QtCore.QRect(250, 120, 31, 23))
self.pushButton_5.setObjectName("pushButton_5")
self.lcdNumber_2 = QtWidgets.QLCDNumber(Form)
self.lcdNumber_2.setGeometry(QtCore.QRect(145, 9, 171, 31))
self.lcdNumber_2.setObjectName("lcdNumber_2")
self.lcdNumber_2.setDigitCount(11)
self.lcdNumber = QtWidgets.QLCDNumber(Form)
self.lcdNumber.setGeometry(QtCore.QRect(145, 59, 171, 31))
self.lcdNumber.setObjectName("lcdNumber")
self.lcdNumber.setDigitCount(8)
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(9, 67, 105, 19))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Cheeky Timer"))
self.pushButton_7.setText(_translate("Form", "-.5"))
self.pushButton_9.setText(_translate("Form", "-.1"))
self.pushButton_6.setText(_translate("Form", "+.1"))
self.pushButton_3.setText(_translate("Form", "+1"))
self.label.setText(_translate("Form", "NTP Time:"))
self.pushButton_8.setText(_translate("Form", "-1"))
self.pushButton_4.setText(_translate("Form", "Reset Time"))
self.pushButton_5.setText(_translate("Form", "+.5"))
self.label_2.setText(_translate("Form", "Current Time:"))
def i_am_working(self):
print('I am working')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
c = ContLCDClock()
c.show()
sys.exit(app.exec_())