Skip to content

Instantly share code, notes, and snippets.

@afzafri
Last active December 5, 2016 05:43
Show Gist options
  • Save afzafri/5a2c78ce9adaa747489c8cef19646aa1 to your computer and use it in GitHub Desktop.
Save afzafri/5a2c78ce9adaa747489c8cef19646aa1 to your computer and use it in GitHub Desktop.
Simple GUI Python Program. Python 2.7 + PySide QT
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>studinfo</class>
<widget class="QWidget" name="studinfo">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>344</width>
<height>524</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="windowTitle">
<string>Student Information Form</string>
</property>
<widget class="QLineEdit" name="input_name">
<property name="geometry">
<rect>
<x>120</x>
<y>80</y>
<width>191</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_name">
<property name="geometry">
<rect>
<x>40</x>
<y>80</y>
<width>61</width>
<height>16</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>Name :</string>
</property>
</widget>
<widget class="QLabel" name="label_age">
<property name="geometry">
<rect>
<x>40</x>
<y>120</y>
<width>61</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Age :</string>
</property>
</widget>
<widget class="QLabel" name="label_gender">
<property name="geometry">
<rect>
<x>40</x>
<y>160</y>
<width>71</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Gender :</string>
</property>
</widget>
<widget class="QLineEdit" name="input_age">
<property name="geometry">
<rect>
<x>120</x>
<y>120</y>
<width>51</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QComboBox" name="select_gender">
<property name="geometry">
<rect>
<x>120</x>
<y>160</y>
<width>69</width>
<height>22</height>
</rect>
</property>
<item>
<property name="text">
<string>Male</string>
</property>
</item>
<item>
<property name="text">
<string>Female</string>
</property>
</item>
</widget>
<widget class="QPushButton" name="save_but">
<property name="geometry">
<rect>
<x>140</x>
<y>200</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Save</string>
</property>
</widget>
<widget class="QTextEdit" name="output_box">
<property name="geometry">
<rect>
<x>40</x>
<y>290</y>
<width>271</width>
<height>171</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_output">
<property name="geometry">
<rect>
<x>40</x>
<y>260</y>
<width>61</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Output :</string>
</property>
</widget>
<widget class="QLabel" name="label_title">
<property name="geometry">
<rect>
<x>40</x>
<y>10</y>
<width>281</width>
<height>51</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>16</pointsize>
<weight>75</weight>
<bold>true</bold>
<underline>true</underline>
</font>
</property>
<property name="text">
<string>Student Information Form</string>
</property>
</widget>
<widget class="QPushButton" name="cleart_but">
<property name="geometry">
<rect>
<x>80</x>
<y>480</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Clear</string>
</property>
</widget>
<widget class="QPushButton" name="exit_but">
<property name="geometry">
<rect>
<x>180</x>
<y>480</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Exit</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'C:\Users\Afif\Desktop\Python\StudentInfo\studform.ui'
#
# Created: Mon Dec 05 02:32:06 2016
# by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!
from PySide import QtCore, QtGui
class Ui_studinfo(object):
# create/setup UI
def setupUi(self, studinfo):
studinfo.setObjectName("studinfo")
studinfo.resize(344, 524)
font = QtGui.QFont()
font.setPointSize(12)
studinfo.setFont(font)
self.input_name = QtGui.QLineEdit(studinfo)
self.input_name.setGeometry(QtCore.QRect(120, 80, 191, 21))
self.input_name.setObjectName("input_name")
self.label_name = QtGui.QLabel(studinfo)
self.label_name.setGeometry(QtCore.QRect(40, 80, 61, 16))
font = QtGui.QFont()
font.setPointSize(12)
self.label_name.setFont(font)
self.label_name.setObjectName("label_name")
self.label_age = QtGui.QLabel(studinfo)
self.label_age.setGeometry(QtCore.QRect(40, 120, 61, 21))
self.label_age.setObjectName("label_age")
self.label_gender = QtGui.QLabel(studinfo)
self.label_gender.setGeometry(QtCore.QRect(40, 160, 71, 16))
self.label_gender.setObjectName("label_gender")
self.input_age = QtGui.QLineEdit(studinfo)
self.input_age.setGeometry(QtCore.QRect(120, 120, 51, 20))
self.input_age.setObjectName("input_age")
self.select_gender = QtGui.QComboBox(studinfo)
self.select_gender.setGeometry(QtCore.QRect(120, 160, 69, 22))
self.select_gender.setObjectName("select_gender")
self.select_gender.addItem("")
self.select_gender.addItem("")
self.save_but = QtGui.QPushButton(studinfo)
self.save_but.setGeometry(QtCore.QRect(140, 200, 75, 23))
self.save_but.setObjectName("save_but")
self.output_box = QtGui.QTextEdit(studinfo)
self.output_box.setGeometry(QtCore.QRect(40, 290, 271, 171))
self.output_box.setObjectName("output_box")
self.label_output = QtGui.QLabel(studinfo)
self.label_output.setGeometry(QtCore.QRect(40, 260, 61, 16))
self.label_output.setObjectName("label_output")
self.label_title = QtGui.QLabel(studinfo)
self.label_title.setGeometry(QtCore.QRect(40, 10, 281, 51))
font = QtGui.QFont()
font.setPointSize(16)
font.setWeight(75)
font.setUnderline(True)
font.setBold(True)
self.label_title.setFont(font)
self.label_title.setObjectName("label_title")
self.clear_but = QtGui.QPushButton(studinfo)
self.clear_but.setGeometry(QtCore.QRect(80, 480, 75, 23))
self.clear_but.setObjectName("clear_but")
self.exit_but = QtGui.QPushButton(studinfo)
self.exit_but.setGeometry(QtCore.QRect(180, 480, 75, 23))
self.exit_but.setObjectName("exit_but")
self.retranslateUi(studinfo)
QtCore.QMetaObject.connectSlotsByName(studinfo)
# call event register function, for listening to any event
self.eventregister()
#translate ui, give label name etc
def retranslateUi(self, studinfo):
studinfo.setWindowTitle(QtGui.QApplication.translate("studinfo", "Student Information Form", None, QtGui.QApplication.UnicodeUTF8))
self.label_name.setText(QtGui.QApplication.translate("studinfo", "Name :", None, QtGui.QApplication.UnicodeUTF8))
self.label_age.setText(QtGui.QApplication.translate("studinfo", "Age :", None, QtGui.QApplication.UnicodeUTF8))
self.label_gender.setText(QtGui.QApplication.translate("studinfo", "Gender :", None, QtGui.QApplication.UnicodeUTF8))
self.select_gender.setItemText(0, QtGui.QApplication.translate("studinfo", "Male", None, QtGui.QApplication.UnicodeUTF8))
self.select_gender.setItemText(1, QtGui.QApplication.translate("studinfo", "Female", None, QtGui.QApplication.UnicodeUTF8))
self.save_but.setText(QtGui.QApplication.translate("studinfo", "Save", None, QtGui.QApplication.UnicodeUTF8))
self.label_output.setText(QtGui.QApplication.translate("studinfo", "Output :", None, QtGui.QApplication.UnicodeUTF8))
self.label_title.setText(QtGui.QApplication.translate("studinfo", "Student Information Form", None, QtGui.QApplication.UnicodeUTF8))
self.clear_but.setText(QtGui.QApplication.translate("studinfo", "Clear", None, QtGui.QApplication.UnicodeUTF8))
self.exit_but.setText(QtGui.QApplication.translate("studinfo", "Exit", None, QtGui.QApplication.UnicodeUTF8))
# event register function
def eventregister(self):
# register event, when user click the the button, call function for display confirmation dialog
# use "lambda" to pass extra arguments
self.save_but.clicked.connect(lambda: self.confirmbox("save"))
self.clear_but.clicked.connect(lambda: self.confirmbox("clear"))
self.exit_but.clicked.connect(lambda: self.confirmbox("exit"))
# main process function
def mainprocess(self):
# get all user input, store into var
name = self.input_name.text()
age = self.input_age.text()
gender = self.select_gender.currentText()
# format the output
output = "Name: " + name + "\nAge: " + age + "\nGender: " + gender
# output to output box
self.output_box.setText(output)
# confirmation message box
def confirmbox(self,type):
mess = ""
if type == "save":
mess = "Save data?"
elif type == "clear":
mess = "Clear data?"
elif type == "exit":
mess = "Exit program?"
# define message box attributes
msgBox = QtGui.QMessageBox() # create new message box
msgBox.setIcon(QtGui.QMessageBox.Question) # set icon "?"
msgBox.setText(mess) # set message text
msgBox.setWindowTitle("Confirm") # set window title
msgBox.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel) # set buttons
clickedval = msgBox.exec_() #get value of clicked button
# value for Ok = 1024, Cancel = 4194304
if clickedval == 1024:
if type == "save":
self.mainprocess() #if yes clicked, call main process function to save
elif type == "clear":
self.output_box.setText("") #if yes clicked, clear output box
elif type == "exit":
sys.exit() #if yes clicked, exit program
else:
pass #if clicked cancel, do nothing
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
studinfo = QtGui.QWidget()
ui = Ui_studinfo()
ui.setupUi(studinfo)
studinfo.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment