|
# A demo of how to get notifications that the state of a checkbox has
# changed as well as echoing the change using a QTextEdit. This demo
# also shows how to change the size of the Dialog.
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import os
import math
class Demo_QCheckbox_QTextEdit(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.setWindowTitle("Demo QCheckbox - Show/Hide QTextEdit")
self.setFixedSize(450, 100)
# Create and set the main vertical layout.
vlayout = QVBoxLayout()
self.setLayout(vlayout)
self.addCheckboxPanel(vlayout)
self.addTextEditPanel(vlayout) # initially hidden
# Ensure the two panels are pushed to the upper edge of the
# Dialog window.
vlayout.addStretch()
self.show()
#--------------------------------------------------------------
def addTextEditPanel(self, parentLayout):
self.textEdit = QTextEdit(self)
self.textEdit.setReadOnly(True)
self.textEdit.setLineWrapMode(QTextEdit.NoWrap)
self.textEdit.resize(400,400)
self.textEdit.setVisible(False)
font = self.textEdit.font()
font.setFamily("Courier")
font.setPointSize(10)
parentLayout.addWidget(self.textEdit)
#--------------------------------------------------------------
def addCheckboxPanel(self, parentLayout):
hlayout = QHBoxLayout()
checkbox = QCheckBox('Show TextEdit')
checkbox.stateChanged.connect(self.chechboxAction)
hlayout.addWidget(checkbox)
hlayout.addStretch()
parentLayout.addLayout(hlayout)
#--------------------------------------------------------------
# Activated by user selecting or deselecting the checkbox.
def chechboxAction(self):
cb = self.sender()
if cb.isChecked():
self.textEdit.setVisible(True)
self.setFixedSize(450, 300)
self.textEdit.resize(400,200)
self.textEdit.append('Checkbox has been selected')
else:
self.textEdit.setVisible(False)
self.setFixedSize(450, 100)
self.textEdit.append('Checkbox has been deselected\n')
# ========================================================
if __name__ == '__main__':
# Create the Qt Application
app = QApplication(sys.argv)
demo = Demo_QCheckbox_QTextEdit() # <<-- Create an instance
demo.show()
sys.exit(app.exec_())
|