Python count How many Clicks are in a second

前端 未结 2 1885
野的像风
野的像风 2021-01-21 04:21

Would someone mind helping me with this? I\'m trying to make a clicks per second test like this one on this website.

I\'ve tried many different things, but I couldn\'t f

相关标签:
2条回答
  • 2021-01-21 04:44

    My Solution:

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

    After clicked:

    0 讨论(0)
  • 2021-01-21 05:05

    You have to assign function to button

    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_()
    
    0 讨论(0)
提交回复
热议问题