Python计数一秒钟内有多少次点击

时间:2016-12-24 04:38:33

标签: python pyqt4

有人会介意帮我这个吗?我试图在this website上进行每秒点击次数测试。

我尝试了很多不同的事情,但我无法弄清楚

这是我的导入

from PyQt4.QtCore import QSize
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QWidget, QIcon, QLabel, QPainter, QPixmap

这是我的代码

class UICPS(QWidget): #||| CPS TAB |||

    def __init__(self, parent=None):
        super(UICPS, self).__init__(parent)
        clicks = 0
        self.Back = QPushButton("<- Back", self)
        self.Back.resize(50,25)
        self.Back.move(0, 425)
        self.Clicked = QLabel(str(clicks), self)
        self.Clicked.move(200, 200)
        self.CPSBTN = QPushButton("Click Me!", self)
        self.CPSBTN.resize(400, 175)
        self.CPSBTN.move(0, 250)

        if self.Clicked:
            clicks +1

enter image description here

2 个答案:

答案 0 :(得分:1)

我的解决方案:

import sys

from PyQt4.QtCore import QTimer
from PyQt4.QtGui import QApplication, QLabel, QPushButton, QWidget

class UICPS(QWidget):
    def __init__(self, parent=None):
        super(UICPS, self).__init__(parent)
        self.clicks = 0
        self.Back = QPushButton("<- Back", self)
        self.Back.resize(50, 25)
        self.Back.move(0, 425)
        self.ClickedLB = QLabel(str(self.clicks), self)
        self.ClickedLB.resize(400, 20)
        self.ClickedLB.move(200, 100)

        self.ClickedLB2 = QLabel(str(self.clicks), self)
        self.ClickedLB2.resize(400, 20)
        self.ClickedLB2.move(200, 150)

        self.ClickedLB3 = QLabel(str(self.clicks), self)
        self.ClickedLB3.resize(400, 20)
        self.ClickedLB3.move(200, 200)

        self.CPSBTN = QPushButton("Click Me!", self)
        self.CPSBTN.clicked.connect(self.Clicked)
        self.CPSBTN.resize(400, 175)
        self.CPSBTN.move(0, 250)
        self.resize(400, 450)

        self.starting = False
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.updateClock)
        self.counter = 0
        self.isFist = True

    def updateClock(self):
        self.counter += 0.01
        self.ClickedLB2.setText("%.2f s" % self.counter)
        if self.counter >= 9.99:
            self.starting = False
            cps = self.clicks/10
            self.ClickedLB3.setText("%.2f CPS" % cps)
            self.timer.stop()

    def Clicked(self):
        if self.starting:
            self.clicks += 1
            self.ClickedLB.setText(str(self.clicks))
        else:
            if self.isFist:
                self.timer.start(10)
                self.starting = True
                self.isFist = False

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = UICPS()
    w.show()
    sys.exit(app.exec_())

enter image description here

点击后:

enter image description here

答案 1 :(得分:0)

您必须将功能分配给按钮

from PyQt4 import QtGui
import sys

class MyWindow(QtGui.QWidget):

    def __init__(self, parent=None):
        super(MyWindow, self).__init__()

        self.clicks = 0

        self.vbox = QtGui.QVBoxLayout()
        self.setLayout(self.vbox)

        self.label = QtGui.QLabel(str(self.clicks), self)
        self.vbox.addWidget(self.label)

        self.button = QtGui.QPushButton("Click Me!", self)
        self.vbox.addWidget(self.button)

        # assign function to button 
        self.button.clicked.connect(self.on_click)

        self.show()

    def on_click(self):
        self.clicks += 1
        self.label.setText(str(self.clicks))

app = QtGui.QApplication(sys.argv)
win = MyWindow()
app.exec_()