Skip to content

Instantly share code, notes, and snippets.

@defron
Created November 19, 2015 05:10
Show Gist options
  • Save defron/1cc56cdef08b06f00e3e to your computer and use it in GitHub Desktop.
Save defron/1cc56cdef08b06f00e3e to your computer and use it in GitHub Desktop.
PyQT learning exercise 1 solution
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>300</width>
<height>150</height>
</rect>
</property>
<property name="windowTitle">
<string>TestDialog</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>80</x>
<y>70</y>
<width>131</width>
<height>61</height>
</rect>
</property>
<property name="text">
<string>Push Me to
enabled/disable
the checkbox</string>
</property>
</widget>
<widget class="QCheckBox" name="checkBox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>90</x>
<y>20</y>
<width>121</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Disabled Checkbox</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
__author__ = 'Defron'
import sys
from PyQt4 import QtCore, QtGui, uic
class MyWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.ui = uic.loadUi('exercise1.ui', self)
self.ui.pushButton.clicked.connect(self.toggle_checkbox)
# self.connect(self.ui.pushButton, QtCore.SIGNAL('clicked()'), self.toggleCheckbox)
def toggle_checkbox(self):
if(self.ui.checkBox.isEnabled()):
self.ui.checkBox.setDisabled(True)
self.ui.checkBox.setText("Disabled Checkbox")
self.ui.checkBox.toggle()
else:
self.ui.checkBox.setEnabled(True)
self.ui.checkBox.setText("Enabled Checkbox")
self.ui.checkBox.toggle()
if(__name__ == '__main__'):
app = QtGui.QApplication(sys.argv)
window = MyWidget()
window.show()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment