Python :: Aufgabe #235
3 Lösungen
Funktionswerte einer Parabel berechnen - Windows Forms
Anfänger - Python
von paddlboot
- 04.12.2019 um 16:13 Uhr
Lösungen:
Hallo,
ich habe mir das GUI mit PyQt, genauer gesagt mit Qt-Designer, gebastelt.
Den Code selber habe ich dann in PyCharm geschrieben.
Da es mein erstes Programm in Python mit einer graphischen Oberfläche ist, bitte ich Fehler zu entschuldigen.
Tipps und Tricks sind dagegen jederzeit willkommen.
ich habe mir das GUI mit PyQt, genauer gesagt mit Qt-Designer, gebastelt.
Den Code selber habe ich dann in PyCharm geschrieben.
Da es mein erstes Programm in Python mit einer graphischen Oberfläche ist, bitte ich Fehler zu entschuldigen.
Tipps und Tricks sind dagegen jederzeit willkommen.
Python-Code
import sys import PyQt5.QtWidgets as widgets import PyQt5.uic as uic import matplotlib.pyplot as plt import numpy as np app = widgets.QApplication(sys.argv) w = uic.loadUi("mainwindow.ui") def loeschetext(): w.startwert.setText("0") w.endwert.setText("0") w.schrittweite.setText("0") def berechnen(): start = w.startwert.text() schritt = w.schrittweite.text() ende = w.endwert.text() print(start) print(schritt) print(ende) s = float(start) e = float(ende) i = float(start) while i <= e: x = i b = float(x) y = b * b print(x) print(y) w.listWidget.addItem(" X-Wert: " + str(x) + " Y-Wert: " + str(y)) i = i + float(schritt) A = np.linspace(s,e, endpoint=True) F1 = A * A plt.plot(A, F1) plt.show() w.pushButtonclear.clicked.connect(loeschetext) w.pushButtonstart.clicked.connect(berechnen) w.show() sys.exit(app.exec_())
XML-Code
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>598</width> <height>374</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QLineEdit" name="startwert"> <property name="geometry"> <rect> <x>140</x> <y>80</y> <width>113</width> <height>20</height> </rect> </property> <property name="text"> <string>1</string> </property> <property name="clearButtonEnabled"> <bool>false</bool> </property> </widget> <widget class="QLineEdit" name="endwert"> <property name="geometry"> <rect> <x>140</x> <y>130</y> <width>113</width> <height>20</height> </rect> </property> <property name="text"> <string>1</string> </property> </widget> <widget class="QLineEdit" name="schrittweite"> <property name="geometry"> <rect> <x>140</x> <y>170</y> <width>113</width> <height>20</height> </rect> </property> <property name="text"> <string>1</string> </property> </widget> <widget class="QLabel" name="label1"> <property name="geometry"> <rect> <x>80</x> <y>80</y> <width>47</width> <height>13</height> </rect> </property> <property name="text"> <string>Startwert</string> </property> </widget> <widget class="QLabel" name="label2"> <property name="geometry"> <rect> <x>80</x> <y>130</y> <width>47</width> <height>13</height> </rect> </property> <property name="text"> <string>Endwert</string> </property> </widget> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>60</x> <y>170</y> <width>71</width> <height>16</height> </rect> </property> <property name="text"> <string>Schrittweite</string> </property> </widget> <widget class="QPushButton" name="pushButtonstart"> <property name="geometry"> <rect> <x>10</x> <y>240</y> <width>75</width> <height>23</height> </rect> </property> <property name="text"> <string>start</string> </property> </widget> <widget class="QPushButton" name="pushButtonclear"> <property name="geometry"> <rect> <x>100</x> <y>240</y> <width>75</width> <height>23</height> </rect> </property> <property name="text"> <string>löschen</string> </property> </widget> <widget class="QListWidget" name="listWidget"> <property name="geometry"> <rect> <x>280</x> <y>80</y> <width>256</width> <height>192</height> </rect> </property> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>598</width> <height>21</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/> </ui>
Zur Implementierung der GUI habe ich PySimpleGUI genutzt. Basierend auf tkinter ist PySimpleGUI aus meiner Sicht eine einfache und pragmatische Lösung, um schnell eine grafische Benutzeroberfläche mit wenig Code zu kreieren.
Installation über
pip install pysimplegui
oder
conda install pysimplegui.
Alles weitere unter https://pysimplegui.readthedocs.io/en/latest/
Installation über
pip install pysimplegui
oder
conda install pysimplegui.
Alles weitere unter https://pysimplegui.readthedocs.io/en/latest/
Python-Code
import PySimpleGUI as sg class Parable: def __init__(self): sg.theme('Default1') sg.SetOptions(font=('Arial', 10), element_padding=(8, 8)) self.start = None self.stop = None self.step = None frame_layout = [ [sg.Text('Startwert', size=(10,1)), sg.InputText(key='_START_', size=(10,1), focus=True)], [sg.Text('Endwert', size = (10,1)), sg.InputText(key='_STOP_', size=(10,1))], [sg.Text('Schrittweite', size = (10,1)), sg.InputText(key='_STEP_', size=(10,1))]] col = [ [sg.Frame('X-Werte', frame_layout, font=('Arial', 8))], [sg.Button('Berechnen', size=(10,2),pad=(15,8), bind_return_key=True), sg.Button('Reset', size=(10,2), pad=(12,8))]] self.layout = [ [sg.Column(col), sg.Multiline('', key='_OUTPUT_', size=(30,12), font=('Consolas', 9))]] self.window = sg.Window('Funktionsweise einer Parabel berechnen (y = x*x)', self.layout) self.loop() def loop(self): while True: correct_input = True event, values = self.window.read() if event in (None, 'Cancel'): break if event == 'Berechnen': try: self.start = int(values['_START_']) except ValueError: sg.Popup('Bitte eine Ganzzahl als Startwert eingeben!', title='Hinweis', font=('Arial', 9)) self.window.FindElement('_START_').Update('') correct_input = False try: self.stop = int(values['_STOP_']) except ValueError: sg.Popup('Bitte eine Ganzzahl als Endwert eingeben!', title='Hinweis', font=('Arial', 9)) self.window.FindElement('_STOP_').Update('') correct_input = False try: self.step = int(values['_STEP_']) except ValueError: sg.Popup('Bitte eine Ganzzahl als Schrittweite eingeben!', title='Hinweis', font=('Arial', 9)) self.window.FindElement('_STEP_').Update('') correct_input = False if correct_input: if self.stop <= self.start: sg.Popup('Der Endwert muss größer als der Startwert sein!', title='Hinweis', font=('Arial', 9)) else: self.window.FindElement('_OUTPUT_').Update(self.calculate_values()) if event == 'Reset': elements = '_START_ _STOP_ _STEP_ _OUTPUT_'.split() for element in elements: self.window.FindElement(element).Update('') def calculate_values(self): return_value = 'Berechnete Werte\n----------------\n' for index in range(self.start, self.stop + 1, self.step): return_value += f'x: {index:7.1f} / y: {index ** 2:7.1f}\n' return return_value Parable()
Python-Code
import sys from PyQt5 import QtWidgets as qtw from PyQt5 import QtGui as qtg from PyQt5 import QtCore as qtc from PyQt5 import uic class Fenster(qtw.QMainWindow): def __init__(self): super().__init__() self.ui=uic.loadUi("funktionswerte.ui", self) self.listWidget.addItem("Berechnete Werte") self.listWidget.addItem("----------------------") self.pushButton.pressed.connect(self.rechnen) self.pushButton_2.pressed.connect(self.reset) def rechnen(self): if self.startwert.text() == "" or self.endwert.text() == "": msg = qtw.QMessageBox.warning(self,"Eingaben","Start und Endwert müssen angegeben werden!") return if self.startwert.text() > self.endwert.text(): msg = qtw.QMessageBox.warning(self,"Eingaben","Startwert muß kleiner sein als der Endwert") return self.listWidget.clear() self.listWidget.addItem("Berechnete Werte") self.listWidget.addItem("----------------------") start=int(self.startwert.text()) ende =int(self.endwert.text()) if self.schrittweite.text()=="": self.schrittweite.setText("1") schritt = int(self.schrittweite.text()) for i in range(start,ende+schritt,schritt): string = "x: " + str(i) + " y: " + str(i**2) self.listWidget.addItem(string) def reset(self): self.startwert.setText("") self.endwert.setText("") self.schrittweite.setText("") self.listWidget.clear() self.listWidget.addItem("Berechnete Werte") self.listWidget.addItem("----------------------") if __name__ == '__main__': app = qtw.QApplication(sys.argv) fenster=Fenster() fenster.show() app.exec_()
XML-Code
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>571</width> <height>314</height> </rect> </property> <property name="windowTitle"> <string>Funktionserte einer Parabel berechnen</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QGroupBox" name="groupBox"> <property name="geometry"> <rect> <x>50</x> <y>30</y> <width>221</width> <height>201</height> </rect> </property> <property name="font"> <font> <pointsize>12</pointsize> </font> </property> <property name="title"> <string>X-Werte</string> </property> <widget class="QWidget" name="formLayoutWidget"> <property name="geometry"> <rect> <x>20</x> <y>50</y> <width>191</width> <height>141</height> </rect> </property> <layout class="QFormLayout" name="formLayout"> <property name="verticalSpacing"> <number>20</number> </property> <item row="0" column="0"> <widget class="QLabel" name="label"> <property name="font"> <font> <pointsize>12</pointsize> <weight>50</weight> <bold>false</bold> </font> </property> <property name="text"> <string>Startwert</string> </property> </widget> </item> <item row="0" column="1"> <widget class="QLineEdit" name="startwert"/> </item> <item row="1" column="0"> <widget class="QLabel" name="label_2"> <property name="font"> <font> <pointsize>12</pointsize> <weight>50</weight> <bold>false</bold> </font> </property> <property name="text"> <string>Endwert</string> </property> </widget> </item> <item row="1" column="1"> <widget class="QLineEdit" name="endwert"/> </item> <item row="2" column="0"> <widget class="QLabel" name="label_3"> <property name="font"> <font> <pointsize>12</pointsize> <weight>50</weight> <bold>false</bold> </font> </property> <property name="text"> <string>Schrittweite</string> </property> </widget> </item> <item row="2" column="1"> <widget class="QLineEdit" name="schrittweite"> <property name="placeholderText"> <string>1</string> </property> </widget> </item> </layout> </widget> </widget> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>70</x> <y>240</y> <width>75</width> <height>41</height> </rect> </property> <property name="text"> <string>Berechnen</string> </property> </widget> <widget class="QPushButton" name="pushButton_2"> <property name="geometry"> <rect> <x>170</x> <y>240</y> <width>75</width> <height>41</height> </rect> </property> <property name="text"> <string>Reset</string> </property> </widget> <widget class="QListWidget" name="listWidget"> <property name="enabled"> <bool>true</bool> </property> <property name="geometry"> <rect> <x>360</x> <y>30</y> <width>191</width> <height>271</height> </rect> </property> <property name="font"> <font> <pointsize>12</pointsize> </font> </property> </widget> </widget> </widget> <resources/> <connections/> </ui>