KI Elemente

Viele Teile ergeben ein Ganzes! Die Kunst unwichtige Details zu vermeiden!

Menues

Datei: MeinTextEditor.py

# Demo einfacher Texteditor
# 2021 Ehrenfried Stuhlpfarrer

# PyQt Bibliotheken importieren
from PyQt5.QtWidgets import *
# Instanz der GUI Klasse
app = QApplication([])
# Angezeigter Fenstername
app.setApplicationName("Mein Text Editor")
# Textfeld definieren
text = QPlainTextEdit()
# Textfeld anzeigen
text.show()
# Anwendung ausführen
app.exec_()
# Demo einfacher Texteditor
# 2021 Ehrenfried Stuhlpfarrer

# PyQt Bibliotheken importieren
from PyQt5.QtWidgets import *
# Instanz der GUI Klasse
app = QApplication([])
# Angezeigter Fenstername
app.setApplicationName("Mein Text Editor")
# Textfeld definieren
text = QPlainTextEdit()
# Fenster definieren
window = QMainWindow()
# Fenstertitel festlegen
window.setWindowTitle("Text Editor")
# Textfeld im Fenster zentrieren
window.setCentralWidget(text)
# Datei - Menü definieren
menu = window.menuBar().addMenu("&Datei")
# Ereignis close definieren - Tastenkürzel
close = QAction("S&chliessen")
# close Ereignis auslösen - Signal
close.triggered.connect(window.close)
# Aktion close dem Menü zuweisen
menu.addAction(close)
# Fenster anzeigen
window.show()
# Anwendung ausführen
app.exec_()

Das Hilfe - Menü implementieren:
Vor #Fenster anzeigen einfügen:
# Hilfe Menü - About Dialog ----------------------------
help_menu = window.menuBar().addMenu("&Hilfe")
about_action = QAction("&Ueber")
help_menu.addAction(about_action)
def show_about_dialog():
text = "<center>" \
"<h1>Text Editor</h1>" \
"&#8291;" \
"<img src=EinVectorIcon.svg>" \
"</center>" \
"<p>Version 27.1.2021<br/>" \
"Copyright &copy; Die Firma Soundso ....</p>"
QMessageBox.about(window, "About Text Editor", text)
about_action.triggered.connect(show_about_dialog)
# ------------------------------------------------------

Datei öffnen:
# Datei öffnen -----------------------------------------
open_action = QAction("&Oeffnen")
def open_file():
path = QFileDialog.getOpenFileName(window, "Oeffnen")[0]
if path:
text.setPlainText(open(path).read())
open_action.triggered.connect(open_file)
menu.addAction(open_action)
# ------------------------------------------------------

Tasten - Kurzkombinationen:
# Tastatur Schnellzugriff
from PyQt5.QtGui import QKeySequence
....
# Datei öffnen -----------------------------------------
open_action = QAction("&Oeffnen")
def open_file():
path = QFileDialog.getOpenFileName(window, "Oeffnen")[0]
if path:
text.setPlainText(open(path).read())
open_action.triggered.connect(open_file)
menu.addAction(open_action)
open_action.setShortcut(QKeySequence.Open)
# ------------------------------------------------------
Datei speichern unter:
# Datei speichern unter --------------------------------
save_as_action = QAction("Speichern &unter...")
def save_as():
path = QFileDialog.getSaveFileName(window, "Speichern unter")[0]
if path:
with open(path, "w") as f:
f.write(text.toPlainText())
save_as_action.triggered.connect(save_as)
menu.addAction(save_as_action)
# ------------------------------------------------------

Datei speichern:
# Datei speichern --------------------------------------
file_path = None
save_action = QAction("&Speichern")
def save():
if file_path is None:
save_as()
else:
with open(file_path, "w") as f:
f.write(text.toPlainText())
save_action.triggered.connect(save)
save_action.setShortcut(QKeySequence.Save)
menu.addAction(save_action)
# ------------------------------------------------------

Kleine Änderungen, damit beim Öffnen und Speichern unter, der aktuelle Pfad verwendet wird:
# Datei öffnen -----------------------------------------
open_action = QAction("&Oeffnen")
global file_path
def open_file():
path = QFileDialog.getOpenFileName(window, "Oeffnen")[0]
if path:
text.setPlainText(open(path).read())
file_path = path
open_action.triggered.connect(open_file)
menu.addAction(open_action)
open_action.setShortcut(QKeySequence.Open)
# ------------------------------------------------------
....
# Datei speichern unter --------------------------------
save_as_action = QAction("Speichern &unter...")
global file_path
def save_as():
path = QFileDialog.getSaveFileName(window, "Speichern unter")[0]
if path:
file_path = path
save()
save_as_action.triggered.connect(save_as)
menu.addAction(save_as_action)
Hier noch einmal der komplette Code:
# Demo einfacher Texteditor
# 2021 Ehrenfried Stuhlpfarrer

# PyQt Bibliotheken importieren
from PyQt5.QtWidgets import *
# Tastatur Schnellzugriff
from PyQt5.QtGui import QKeySequence
# Instanz der GUI Klasse
app = QApplication([])
# Angezeigter Fenstername
app.setApplicationName("Mein Text Editor")
# Textfeld definieren
text = QPlainTextEdit()
# Fenster definieren
window = QMainWindow()
# Fenstertitel festlegen
window.setWindowTitle("Text Editor")
# Textfeld im Fenster zentrieren
window.setCentralWidget(text)
# Datei - Menü definieren
menu = window.menuBar().addMenu("&Datei")
# Ereignis close definieren - Tastenkürzel
close = QAction("S&chliessen")
# close Ereignis auslösen - Signal
close.triggered.connect(window.close)
# Aktion close dem Menü zuweisen
menu.addAction(close)
# Hilfe Menü - About Dialog ----------------------------
help_menu = window.menuBar().addMenu("&Hilfe")
about_action = QAction("&Ueber")
help_menu.addAction(about_action)
def show_about_dialog():
text = "<center>" \
"<h1>Text Editor</h1>" \
"&#8291;" \
"<img src=EinVectorIcon.svg>" \
"</center>" \
"<p>Version 27.1.2021<br/>" \
"Copyright &copy; Die Firma Soundso ....</p>"
QMessageBox.about(window, "About Text Editor", text)
about_action.triggered.connect(show_about_dialog)
# ------------------------------------------------------
# Datei öffnen -----------------------------------------
open_action = QAction("&Oeffnen")
global file_path
def open_file():
path = QFileDialog.getOpenFileName(window, "Oeffnen")[0]
if path:
text.setPlainText(open(path).read())
file_path = path
open_action.triggered.connect(open_file)
menu.addAction(open_action)
open_action.setShortcut(QKeySequence.Open)
# ------------------------------------------------------
# Datei speichern unter --------------------------------
save_as_action = QAction("Speichern &unter...")
global file_path
def save_as():
path = QFileDialog.getSaveFileName(window, "Speichern unter")[0]
if path:
file_path = path
save()
save_as_action.triggered.connect(save_as)
menu.addAction(save_as_action)
# ------------------------------------------------------
# Datei speichern --------------------------------------
file_path = None
save_action = QAction("&Speichern")
def save():
if file_path is None:
save_as()
else:
with open(file_path, "w") as f:
f.write(text.toPlainText())
save_action.triggered.connect(save)
save_action.setShortcut(QKeySequence.Save)
menu.addAction(save_action)
# ------------------------------------------------------
# ------------------------------------------------------
# Fenster anzeigen
window.show()
# Anwendung ausführen
app.exec_()

KI

Copyright © 2021. All Rights Reserved. Ehrenfried Stuhlpfarrer