Skip to content

Instantly share code, notes, and snippets.

@shredEngineer
Created March 15, 2021 11:03
Show Gist options
  • Save shredEngineer/693f6f992626fc0706f19344e257c5fb to your computer and use it in GitHub Desktop.
Save shredEngineer/693f6f992626fc0706f19344e257c5fb to your computer and use it in GitHub Desktop.
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QDesktopWidget, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton
class GUI(QMainWindow):
def __init__(self):
QMainWindow.__init__(self, flags=Qt.Window)
self.setWindowTitle("Rechner")
self.setFixedSize(300, 300)
geometry = self.frameGeometry()
geometry.moveCenter(QDesktopWidget().availableGeometry().center())
self.move(geometry.topLeft())
self.main_widget = QWidget()
self.setCentralWidget(self.main_widget)
self.main_layout = QVBoxLayout()
self.main_widget.setLayout(self.main_layout)
self.input_box_a = QLineEdit("4000")
self.input_box_a.textChanged.connect(self.on_input_a_changed)
self.main_layout.addWidget(QLabel("Input A:"))
self.main_layout.addWidget(self.input_box_a)
self.main_layout.addSpacing(20)
self.input_box_b = QLineEdit("12")
self.input_box_b.textChanged.connect(self.on_input_b_changed)
self.main_layout.addWidget(QLabel("Input B:"))
self.main_layout.addWidget(self.input_box_b)
self.main_layout.addSpacing(20)
self.result_box = QLineEdit()
self.result_box.setReadOnly(True)
self.main_layout.addWidget(QLabel("Ergebnis A · B:"))
self.main_layout.addWidget(self.result_box)
self.main_layout.addSpacing(20)
self.close_button = QPushButton("Schließen")
self.close_button.clicked.connect(self.on_close_clicked)
self.main_layout.addWidget(self.close_button)
self.main_layout.addStretch()
self.update_result()
def update_result(self):
try:
a = int(self.input_box_a.text())
except ValueError:
a = 0
try:
b = int(self.input_box_b.text())
except ValueError:
b = 0
x = a * b
print(f"Neuberechnung: a = {a}, b = {b}, x = {x}\n")
self.result_box.setText(str(x))
def on_input_a_changed(self, text):
print(f"Input A wurde geändert: {text}")
self.update_result()
def on_input_b_changed(self, text):
print(f"Input B wurde geändert: {text}")
self.update_result()
def on_close_clicked(self):
self.close()
def main():
app = QApplication(sys.argv)
gui = GUI()
gui.show()
rc = app.exec()
sys.exit(rc)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment