Python Forum
[PyQt] Push Button issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Push Button issue
#6
    def __init__(self, *args, **kwargs):
 
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_time)
        …
        self.start_button = QPushButton(self)
        self.start_button.setStyleSheet("QPushButton {background-color: yellow; font-size: 15pt;}")
        self.start_button.setText("Start")
        self.start_button.clicked.connect(self.start_timer)  # Connect button here.  Right after you make it
 
 
        self.my_counter = 0
        self.my_counter1 = 0
        self.my_counter2 = 0
 
 
I have PySide2 instead of Qt5, but they are nearly identical. The signal for the QPushbutton is clicked() instead of click(). Decided to use only one label for the time display 'cause I am lazy.
import sys
from PySide2.QtCore import QTimer
from PySide2.QtWidgets import QWidget, QLabel, QPushButton, QGridLayout, QApplication
 
class Stopwatch(QWidget):
 
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
 
        self.tics = 0
        self.increment = 10
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_time)
        
        self.time_display = QLabel(self, text='00:00:00.00')
        self.time_display.setStyleSheet("QLabel {background-color: white; font-size: 50pt;}")
        
        self.start_button = QPushButton(self)
        self.start_button.setStyleSheet("QPushButton {background-color: yellow; font-size: 20pt;}")
        self.start_button.setText("Start")
        self.start_button.clicked.connect(self.start_timer)
        
        self.layout = QGridLayout(self)
        self.layout.addWidget(self.time_display, 0, 0)
        self.layout.addWidget(self.start_button, 1, 0)
          
    def update_time(self):
        """Increment counter and update time display"""
        # Calculate hours:minutes:seconds
        self.tics += self.increment
        h = self.tics // 3600000
        m = (self.tics // 60000) % 60
        s = (self.tics / 1000) % 60
        self.time_display.setText(f'{h:02d}:{m:02d}:{s:05.2f}')
 
    def start_timer(self):
        """Start/Stop the timer"""
        if self.start_button.text() == 'Start':
            self.tics = 0
            self.start_button.setText('Stop')
            self.timer.start(self.increment)
        else:
            self.timer.stop()
            self.start_button.setText('Start')
  
app = QApplication(sys.argv)
win = Stopwatch()
win.setWindowTitle('Stopwatch')
win.show()
sys.exit(app.exec_())
Reply


Messages In This Thread
Push Button issue - by gvin47 - Apr-16-2020, 06:39 PM
RE: Push Button issue - by deanhystad - Apr-16-2020, 08:59 PM
RE: Push Button issue - by gvin47 - Apr-17-2020, 04:41 AM
RE: Push Button issue - by deanhystad - Apr-17-2020, 08:21 AM
RE: Push Button issue - by gvin47 - Apr-17-2020, 05:30 PM
RE: Push Button issue - by deanhystad - Apr-17-2020, 06:39 PM
RE: Push Button issue - by gvin47 - Apr-17-2020, 07:28 PM
RE: Push Button issue - by gvin47 - Apr-17-2020, 10:59 PM
RE: Push Button issue - by deanhystad - Apr-18-2020, 05:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - touchscreen, push the button like click the mouse John64 5 973 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 5,336 May-25-2023, 07:37 PM
Last Post: deanhystad
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,080 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  Windows GUI with push buttons to launch python scripts drifterf 7 4,258 Jul-17-2019, 05:34 PM
Last Post: Yoriz
  Text after push button chano 13 5,567 Jul-05-2019, 03:10 PM
Last Post: Yoriz
  [PyQt] Close program using Push Button with the help of input from a Message Box bhargavbn 2 6,766 Oct-30-2018, 05:09 AM
Last Post: bhargavbn
  [Tkinter] Selected radio button in push button in Tkinter prashantfunde91 1 11,891 Jun-22-2017, 05:27 PM
Last Post: DeaD_EyE
  PyQt4 get text from line edit into label on button push iFunKtion 2 21,875 Feb-27-2017, 12:14 PM
Last Post: iFunKtion

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020