PyQt5 Button Create And Event

by Atakan

Hi
Let’s look at button operations with PyQt5. What we will do ?

  • Creating a dynamic button
  • Button click event definition

Let’s start

We may need more than one button. I have defined a button maker class to avoid repetition each time.
Like this;

class CreateButton:
    def __init__(self,window,text,width,height,x,y):
        self.button = QtWidgets.QPushButton(window)
        self.button.setText(text)
        self.button.resize(width,height)
        self.button.move(x,y)
    def get_instance(self):
        return self.button

My mainframe class looks like this

class MainFrame(QMainWindow):
    def __init__(self):
        super(MainFrame,self).__init__()
        self.init()
        self.show()

    def toggle_btn1(self):
        if self.b1.isVisible():
            self.b1.setVisible(False)
        else:
            self.b1.setVisible(True)

    def toggle_btn2(self):
        if self.b2.isVisible():
            self.b2.setVisible(False)
        else:
            self.b2.setVisible(True)

    def init(self):
        self.resize(W_WIDTH,W_HEIGHT)
        self.move(W_X,W_Y)
        self.setWindowTitle("www.langpy.com | Python GUI Tutorial")

        self.b1 = CreateButton(self,"Toggle Second Button",120,40,10,10).get_instance()
        self.b2 = CreateButton(self,"Toggle First Button",120,40,10,50).get_instance()

        self.b1.clicked.connect(self.toggle_btn2)
        self.b2.clicked.connect(self.toggle_btn1)

If you pay attention, I have determined the method to be triggered after clicking the button object .clicked .connect method

        self.b1.clicked.connect(self.toggle_btn2)
        self.b2.clicked.connect(self.toggle_btn1)

Full source code

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys

W_WIDTH = 400
W_HEIGHT = 150
W_X = 400
W_Y = 150

class CreateButton:
    def __init__(self,window,text,width,height,x,y):
        self.button = QtWidgets.QPushButton(window)
        self.button.setText(text)
        self.button.resize(width,height)
        self.button.move(x,y)
    def get_instance(self):
        return self.button


class MainFrame(QMainWindow):
    def __init__(self):
        super(MainFrame,self).__init__()
        self.init()
        self.show()

    def toggle_btn1(self):
        if self.b1.isVisible():
            self.b1.setVisible(False)
        else:
            self.b1.setVisible(True)

    def toggle_btn2(self):
        if self.b2.isVisible():
            self.b2.setVisible(False)
        else:
            self.b2.setVisible(True)

    def init(self):
        self.resize(W_WIDTH,W_HEIGHT)
        self.move(W_X,W_Y)
        self.setWindowTitle("www.langpy.com | Python GUI Tutorial")

        self.b1 = CreateButton(self,"Toggle Second Button",120,40,10,10).get_instance()
        self.b2 = CreateButton(self,"Toggle First Button",120,40,10,50).get_instance()

        self.b1.clicked.connect(self.toggle_btn2)
        self.b2.clicked.connect(self.toggle_btn1)



app = QApplication(sys.argv)
win = MainFrame()
sys.exit(app.exec_())

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. OK Read More