analitics

Pages

Friday, December 7, 2018

Python Qt5 : simple checkbox example.

Today we created a simple tutorial about QCheckBox and QLabel.
The purpose of this tutorial is to use QCheckBox in a GUI interface.
When we check QCheckBox, this will change the text from a QLabel.
The variables used by QCheckBox are my_checkbox and my_label for QLabel.
The result of my source code is this:

Let's see the source code:
# -*- coding: utf-8 -*-
"""
@author: catafest
"""
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QCheckBox, QLabel, QApplication

class MyCheckBox(QWidget):
 def __init__(self):
  super().__init__()
 
  my_checkbox = QCheckBox("Check this , see result", self)
  my_checkbox.move(50,60)
  my_checkbox.stateChanged.connect(self.change_my_option)
  

  self.my_label = QLabel("You can visit free-tutorial.org ", self)
  self.my_label.move(50,30)

  #self.my_label.setAlignment(Qt.AlignCenter)
  
  self.setGeometry(420,420,640,100)
  self.setWindowTitle("free-tutorials.org PyQt5 ChecBox ")
  

  
 def change_my_option(self, state):
  if state  == Qt.Checked:
   self.my_label.setText("Thank's by free-tutorial.org")
  else:
   self.my_label.setText("You can visit free-tutorial.org")
   
if __name__ == '__main__':
 app = QApplication(sys.argv)
 win = MyCheckBox()
 win.show()
 sys.exit(app.exec_())