PyQT Check box




In this PyQT application development tutorial, we're going to cover how to add a checkbox to your GUI.

Let's have our checkbox enlarge the window, since having a checkbox exit the application is silly, and we might as well create another method!


checkBox = QtGui.QCheckBox('Enlarge Window', self)
checkBox.move(100, 25)
checkBox.stateChanged.connect(self.enlarge_window)
First we define our checkbox, what we want it so say, then we place it, then we code what we want to happen if the checkbox is toggled.

It should be noted that the way the checkbox works is "on toggle" with the stateChanged method. As such, this will only occur if the state is literally changed.

For example, you can add in a checkBox.toggle() before the stateChanged, and nothing will happen:

        checkBox = QtGui.QCheckBox('Enlarge Window', self)
        checkBox.move(100, 25)
		checkBox.toggle()
        checkBox.stateChanged.connect(self.enlarge_window)
        

The .toggle() will set the box on, otherwise the default is off. If you run this, however, you will find the window is not enlarged. The reason why is because we have first toggled, then defined what to do if toggled. Instead, what you would need to do is:

        checkBox = QtGui.QCheckBox('Enlarge Window', self)
        checkBox.move(100, 25)
        checkBox.stateChanged.connect(self.enlarge_window)
		checkBox.toggle()
        

Then, the window would default to the enlarged state, and have the box checked by default.

Of course, however, nothing will work without the enlarge_window method, so let's make that:


    def enlarge_window(self, state):
        if state == QtCore.Qt.Checked:
            self.setGeometry(50,50, 1000, 600)
        else:
            self.setGeometry(50, 50, 500, 300)
Simple enough code. If the button is checked, then we want to make the window larger, using the setGeometry method. Otherwise, if it is not, make the window smaller. Again, this code will only run if the toggle has its state changed.

The full code up to this point:

import sys
from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("PyQT tuts!")
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))

        extractAction = QtGui.QAction("&GET TO THE CHOPPAH!!!", self)
        extractAction.setShortcut("Ctrl+Q")
        extractAction.setStatusTip('Leave The App')
        extractAction.triggered.connect(self.close_application)

        self.statusBar()

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(extractAction)
        

        self.home()

    def home(self):
        btn = QtGui.QPushButton("Quit", self)
        btn.clicked.connect(self.close_application)
        btn.resize(btn.minimumSizeHint())
        btn.move(0,100)

        extractAction = QtGui.QAction(QtGui.QIcon('todachoppa.png'), 'Flee the Scene', self)
        extractAction.triggered.connect(self.close_application)
        
        self.toolBar = self.addToolBar("Extraction")
        self.toolBar.addAction(extractAction)

        checkBox = QtGui.QCheckBox('Enlarge Window', self)
        checkBox.move(100, 25)
        checkBox.stateChanged.connect(self.enlarge_window)
        # depending on what you want the default to be.
        #checkBox.toggle()
        self.show()

    def enlarge_window(self, state):
        if state == QtCore.Qt.Checked:
            self.setGeometry(50,50, 1000, 600)
        else:
            self.setGeometry(50, 50, 500, 300)
        


    def close_application(self):
        choice = QtGui.QMessageBox.question(self, 'Extract!',
                                            "Get into the chopper?",
                                            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
        if choice == QtGui.QMessageBox.Yes:
            print("Extracting Naaaaaaoooww!!!!")
            sys.exit()
        else:
            pass
        
        

    
def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())


run()

The resulting window:

In the next PyQT tutorial, we're going to cover adding a progress bar to our GUI.

The next tutorial:





  • PyQT Basic Tutorial
  • PyQT Application Structure
  • PyQT buttons
  • Button Functions with PyQT
  • PyQT Menubar
  • PyQT Toolbar
  • Pop up Message PyQT
  • PyQT Check box
  • PyQT Progress bar example
  • PyQT Dropdown button and QT Styles
  • PyQT Font widget
  • PyQT Color picker widget
  • PyQT Text Editor
  • PyQT open files to edit
  • PyQT file saving