Python Forum
Button to +1 in text box everytime it's clicked
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Button to +1 in text box everytime it's clicked
#1
Hello I have the following code that I want it to start counting from 0 adding 1 every time "pushbutton" is pressed and the value to be shown in "textBrowser", is this possible as I am having some trouble?:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(656, 383)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(230, 50, 51, 41))
        self.pushButton.setObjectName("pushButton")

self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.textBrowser.setGeometry(QtCore.QRect(80, 180, 61, 41))
        self.textBrowser.setObjectName("textBrowser")
Gribouillis write May-01-2024, 07:21 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
You must create a window and connect the button to a function.

from PyQt5 import QtWidgets
 
class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)
        self.setup_ui()
    
    def setup_ui(self):
        toolbar = self.addToolBar("Tools")
        self.count_button = QtWidgets.QPushButton("+")
        self.count_button.clicked.connect(self.on_btn_clicked)
        toolbar.addWidget(self.count_button)
 
        self.text_browser = QtWidgets.QTextBrowser()      
        self.text_browser.setText("0")
        self.setCentralWidget(self.text_browser)
        
        self.counter = 1
                
        self.resize(656, 383)
        
    def on_btn_clicked(self):
        self.text_browser.setText(f"{self.counter}")
        self.counter += 1

        
if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    win = MainWindow()
    win.setWindowTitle("MainWindow")
    win.show()
    sys.exit(app.exec_())
or use a Spinbox

from PyQt5 import QtWidgets
 
class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)
        self.setup_ui()
    
    def setup_ui(self):
        toolbar = self.addToolBar("Tools")
        
        self.spin_box = QtWidgets.QSpinBox()
        self.spin_box.valueChanged.connect(self.on_spin_box)
        toolbar.addWidget(self.spin_box)
 
        self.text_browser = QtWidgets.QTextBrowser()      
        self.text_browser.setText("0")
        self.setCentralWidget(self.text_browser)
                
        self.resize(656, 383)
        
    def on_spin_box(self):
        self.text_browser.setText(f"{self.spin_box.value()}")

        
if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    win = MainWindow()
    win.setWindowTitle("MainWindow")
    win.show()
    sys.exit(app.exec_())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't change the colour of Tk button text Pilover 6 15,049 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] Button clicked not working catlessness 10 8,426 Oct-21-2021, 12:36 PM
Last Post: catlessness
  [Tkinter] Make my button text update? Skata100 1 2,087 Aug-07-2021, 05:37 AM
Last Post: deanhystad
  [Tkinter] Hide clicked buttons Rubberduck 6 3,678 Jun-02-2021, 12:44 PM
Last Post: Rubberduck
  Updating button text based upon different variable values knoxvilles_joker 0 2,280 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  tkinter | Button color text on Click Maryan 2 3,448 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [tkinter] not getting checkbutton value when clicked OogieM 5 6,280 Sep-20-2020, 04:49 PM
Last Post: deanhystad
  How to make button text bold in Tkinter? scratchmyhead 2 12,258 May-16-2020, 02:53 AM
Last Post: scratchmyhead
  [PyQt] Avoid clicked event from button when button is not physically selected and clicked mart79 2 2,409 May-05-2020, 12:54 PM
Last Post: mart79
  [Tkinter] Text Button - How Do I Reduce The Margin? vman44 6 11,491 Apr-27-2020, 10:48 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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