ich habe meine erste QT-GUI gebaut un stelle sie hier mal vor, Ich glaube es ist für Anfänger wie ich einer bin ein guter Einstieg.
Da möchten doch auch die Profi´s mal drüberschauen um auf Fehler in der Programmierung hinzuweisen.
Die Gui ist mit dem QT Designer gemacht.
Code: Alles auswählen
import sys
import datetime
import time
from PyQt5 import QtWidgets, uic
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class MeinDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.uix = uic.loadUi("hauptdialog.ui",self)
#self.uix.geburtsdatum.setDate(QDate.fromString( "02/08/2019", "dd/MM/yyyy"))
self.uix.geburtsdatum.setDate(datetime.datetime.now())
#Slots einrichten
self.uix.buttonOK.clicked.connect(self.onOK)
self.uix.buttonAbbrechen.clicked.connect(self.onAbbrechen)
#Progress Bar testen
self.uix.progressBar.reset()
self.uix.progressBar.setMaximum(100)
#Timer
self.timer = QTimer()
self.timer.setSingleShot(False)
self.timer.setInterval(1000)
self.timer.start()
self.timer.timeout.connect(self.timing)
def timing(self):
self.uix.uhrzeit.setText(time.strftime("%d.%m.%Y %H:%M:%S"))
def onOK(self):
#Uhrzeit
#self.uix.uhrzeit.setText(str(datetime.datetime.now()))
#Daten auslesen
if self.uix.vorname.text() == "":
showdialog("Bitte Vornamen eingeben")
#self.uix.vorname.setText("HARYD")
#self.uix.vorname.selectAll()
#self.uix.vorname.setHidden(True)
#self.uix.vorname.setDisabled(True)
#self.uix.vorname.setEnabled(True)
#self.uix.progressBar.setMaximum(100)
self.uix.progressBar.setValue(10)
self.uix.vorname.setFocus()
elif self.uix.nachname.text() == "":
self.uix.progressBar.setValue(25)
showdialog("Bitte Nachnamen eingeben")
self.uix.nachname.setFocus()
elif self.uix.geburtsdatum.date() == datetime.datetime.now() or self.uix.geburtsdatum.date() > datetime.datetime.now():
self.uix.progressBar.setValue(50)
showdialog("Bitte richtiges Geburtsdatum eingeben")
self.uix.geburtsdatum.setFocus()
elif self.uix.adresse.toPlainText() == "":
self.uix.progressBar.setValue(75)
showdialog("Bitte Adresse eingeben")
self.uix.adresse.setFocus()
elif not self.uix.agb.isChecked():
self.uix.progressBar.setValue(100)
showdialog("AGB muss akzeptiert werden")
self.uix.agb.setFocus()
else:
#Mach irgendwas mit den eingegebenen Daten
print ("Vorname: {}".format(self.uix.vorname.text()))
print ("Nachname: {}".format(self.uix.nachname.text()))
print ("Adresse: {}".format(self.uix.adresse.toPlainText()))
datum = self.uix.geburtsdatum.date().toString("dd.MM.yyyy")
print("Geburtsdatum: {}".format(datum))
if self.uix.agb.checkState():
print("AGBs akzeptiert")
if self.uix.katalog.checkState():
print("Katalog bestellt")
self.close()
def onAbbrechen(self):
print ("Abbruch")
self.close()
def showdialog(Fehlermeldung):
msg = QMessageBox()
#msg.setInformativeText("Zusatzinformationen")
#msg.setDetailedText("Im Folgenden die Details:")
msg.setIcon(QMessageBox.Information)
msg.setText(Fehlermeldung)
msg.setWindowTitle("Achtung")
msg.setStandardButtons(QMessageBox.Ok)
#msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
retval = msg.exec_()
#print ("Buttondruck:", retval)
app = QtWidgets.QApplication(sys.argv)
dialog = MeinDialog()
dialog.show()
sys.exit(app.exec_())
Code: Alles auswählen
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>320</width>
<height>383</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QGroupBox" name="groupBox">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>301</width>
<height>231</height>
</rect>
</property>
<property name="title">
<string>Persönliche Daten</string>
</property>
<widget class="QTextEdit" name="adresse">
<property name="geometry">
<rect>
<x>150</x>
<y>120</y>
<width>141</width>
<height>101</height>
</rect>
</property>
<property name="html">
<string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;">
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html></string>
</property>
</widget>
<widget class="QLineEdit" name="nachname">
<property name="geometry">
<rect>
<x>150</x>
<y>50</y>
<width>141</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>42</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Vorname</string>
</property>
<property name="buddy">
<cstring>vorname</cstring>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>10</x>
<y>160</y>
<width>81</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Adresse</string>
</property>
<property name="buddy">
<cstring>adresse</cstring>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>10</x>
<y>80</y>
<width>81</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Geburtsdatum</string>
</property>
<property name="buddy">
<cstring>geburtsdatum</cstring>
</property>
</widget>
<widget class="QDateEdit" name="geburtsdatum">
<property name="geometry">
<rect>
<x>150</x>
<y>80</y>
<width>91</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="vorname">
<property name="geometry">
<rect>
<x>150</x>
<y>20</y>
<width>141</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>10</x>
<y>50</y>
<width>61</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Nachname</string>
</property>
<property name="buddy">
<cstring>nachname</cstring>
</property>
</widget>
</widget>
<widget class="QGroupBox" name="groupBox_2">
<property name="geometry">
<rect>
<x>10</x>
<y>240</y>
<width>301</width>
<height>71</height>
</rect>
</property>
<property name="title">
<string>Weitere Angaben</string>
</property>
<widget class="QCheckBox" name="katalog">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<width>151</width>
<height>18</height>
</rect>
</property>
<property name="text">
<string>Katalog bestellen</string>
</property>
</widget>
<widget class="QCheckBox" name="agb">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>161</width>
<height>18</height>
</rect>
</property>
<property name="text">
<string>AGB gelesen und akzeptiert</string>
</property>
</widget>
</widget>
<widget class="QPushButton" name="buttonOK">
<property name="geometry">
<rect>
<x>160</x>
<y>320</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>OK</string>
</property>
</widget>
<widget class="QPushButton" name="buttonAbbrechen">
<property name="geometry">
<rect>
<x>240</x>
<y>320</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Abbrechen</string>
</property>
</widget>
<widget class="QProgressBar" name="progressBar">
<property name="geometry">
<rect>
<x>10</x>
<y>350</y>
<width>301</width>
<height>23</height>
</rect>
</property>
<property name="value">
<number>24</number>
</property>
</widget>
<widget class="QLabel" name="uhrzeit">
<property name="geometry">
<rect>
<x>10</x>
<y>320</y>
<width>131</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Uhrzeit</string>
</property>
<property name="buddy">
<cstring>adresse</cstring>
</property>
</widget>
</widget>
<tabstops>
<tabstop>buttonOK</tabstop>
<tabstop>buttonAbbrechen</tabstop>
<tabstop>geburtsdatum</tabstop>
<tabstop>vorname</tabstop>
<tabstop>katalog</tabstop>
<tabstop>agb</tabstop>
<tabstop>adresse</tabstop>
<tabstop>nachname</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>