ich steige gerade in das Thema Python ein und bin nun vor einem Problem gestoßen, dass ich seit vielen Stunden versuche zu löse.
Ich möchte für mich ein kleines Tool bauen, welches verschiedene Formen beinhaltet. Ich habe mein Repo wie folgt geordnet:
- docs (Folder)
- gui (Folder)
- researchtool (Folder)
- tests (Folder)
- assets (Folder with images)
- gui_mainmenu.py
- gui_summarization.py
- gui_settings.py
Mein Problem betrifft aber jedoch eher der gui_mainmenu.py und gui_summarization.
Ich möchte über die Form Mainmenu die Form Summarization aufrufen und wenn ich wieder die Form Summarization schließe die Form MainMenu aufgreifen.
Ich habe in der Dokumentation gelesen, dass dies über TopLevel und Klassen funktioniert und habe mich auch mithilfe dieses Tutorials versucht dies für meinen Fall zu lösen. Der einzige Unterschied ist, dass ich dies in Python Skripte trennen möchte. Dies ist für mich einfach wichtig, dass ich GUIs auch als Dateien trenne.
gui_mainmenu.py
Code: Alles auswählen
import tkinter
import gui_summarize
import gui_settings
# With help from this side: https://www.pythontutorial.net/tkinter/tkinter-toplevel/
class GUIMainMenu(tkinter.Toplevel):
def __init__(self):
super().__init__()
self_width = 800
self_height = 500
# center_x, center_y = gui_settings.center_form(self, self_width, self_height)
# self.geometry(f'{self_width}x{self_height}+{center_x}+{center_y}')
self.geometry(f'{self_width}x{self_height}')
self.title("Research Tool")
self.configure(bg="#EEEEEE")
# Background Canvas
canvas = tkinter.Canvas(
self,
bg="#EEEEEE",
height=500,
width=800,
bd=0,
highlightthickness=0,
relief="ridge"
)
canvas.place(x=0, y=0)
canvas.create_rectangle(
0.0,
0.0,
400.0,
500.0,
fill="#00ADB5",
outline="")
# Title, Description (Texts) and Images on Left Side
canvas.create_text(
50.0,
56.0,
anchor="nw",
text="Research Tool",
fill="#EEEEEE",
font=("Roboto", 48 * -1)
)
image_checkbox = tkinter.PhotoImage(
file=gui_settings.assets("checkbox.png"))
image_cb1 = canvas.create_image(
96.0,
208.0,
image=image_checkbox
)
image_cb2 = canvas.create_image(
96.0,
273.0,
image=image_checkbox
)
image_cb3 = canvas.create_image(
96.0,
340.0,
image=image_checkbox
)
image_cb4 = canvas.create_image(
96.0,
406.0,
image=image_checkbox
)
canvas.create_text(
125.0,
194.0,
anchor="nw",
text="Summarize Text",
fill="#FFFFFF",
font=("Roboto", 24 * -1)
)
canvas.create_text(
125.0,
324.0,
anchor="nw",
text="Analyze Keywords",
fill="#FFFFFF",
font=("Roboto", 24 * -1)
)
canvas.create_text(
125.0,
257.0,
anchor="nw",
text="Identify Keywords",
fill="#FFFFFF",
font=("Roboto", 24 * -1)
)
canvas.create_text(
125.0,
390.0,
anchor="nw",
text="Further Research",
fill="#FFFFFF",
font=("Roboto", 24 * -1)
)
canvas.create_text(
7.0,
481.0,
anchor="nw",
text="© (2022) Christoph Beckmann",
fill="#FFFFFF",
font=("Roboto", 12 * -1)
)
# Right Side: Subtitle and Buttons
canvas.create_text(
415.0,
66.0,
anchor="nw",
text="What should be done?",
fill="#222831",
font=("Roboto", 36 * -1)
)
# Buttons
btn_image_Summarize = tkinter.PhotoImage(
file=gui_settings.assets("btn_Summarize.png"))
btn_Summarize = tkinter.Button(
image=btn_image_Summarize,
borderwidth=0,
highlightthickness=0,
command=lambda: self.open_gui_summarize(),
relief="flat"
)
btn_Summarize.place(
x=500.0,
y=197.0,
width=200.0,
height=58.0
)
btn_image_Analyze = tkinter.PhotoImage(
file=gui_settings.assets("btn_Analyze.png"))
btn_Analyze = tkinter.Button(
image=btn_image_Analyze,
borderwidth=0,
highlightthickness=0,
command=lambda: print("Analyze pressed"),
relief="flat"
)
btn_Analyze.place(
x=500.0,
y=288.0,
width=200.0,
height=58.0
)
btn_image_Research = tkinter.PhotoImage(
file=gui_settings.assets("btn_Research.png"))
btn_Research = tkinter.Button(
image=btn_image_Research,
borderwidth=0,
highlightthickness=0,
command=lambda: print("Research pressed"),
relief="flat"
)
btn_Research.place(
x=500.0,
y=379.0,
width=200.0,
height=58.0
)
self.resizable(False, False)
def open_gui_summarize(self):
gui_summarize = gui_summarize.GUISummarize(self)
gui_summarize.grab_set()
if __name__ == "__main__":
mainmenu = GUIMainMenu()
mainmenu.mainloop()
Code: Alles auswählen
import gui_settings
from tkinter import *
import tkinter.filedialog
def open_textfile(textbox: tkinter.Text):
filetypes = (
('AlL files', '*.*')
)
file = tkinter.filedialog.askopenfilename(
filetypes=[filetypes],
defaultextension=".txt")
fob = open(file, 'r')
text = fob.read()
textbox.delete(1.0, tk.END)
textbox.insert(tk.INSERT, text)
fob.close()
class GUISummarize(tkinter.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self_width = 1200
self_height = 800
# center_x, center_y = gui_settings.center_form(self, self_width, self_height)
# self.geometry(f'{self_width}x{self_height}+{center_x}+{center_y}')
self.geometry(f'{self_width}x{self_height}')
self.title("Research Tool - Summarize")
self.configure(bg="#EEEEEE")
canvas = tkinter.Canvas(
self,
bg="#EEEEEE",
height=800,
width=1200,
bd=0,
highlightthickness=0,
relief="ridge"
)
canvas.place(x=0, y=0)
canvas.create_rectangle(
600.0,
0.0,
1200.0,
800.0,
fill="#00ADB5",
outline="")
btn_image_summarize = tkinter.PhotoImage(
file=gui_settings.assets("btn_summarize.png"))
btn_summarize = tkinter.Button(
image=btn_image_summarize,
command=lambda: print("Summarize clicked"),
)
btn_summarize.place(
x=200.0,
y=716.0,
width=200.0,
height=58.0
)
btn_image_export = PhotoImage(
file=gui_settings.assets("btn_export.png"))
btn_export = Button(
image=btn_image_export,
borderwidth=0,
highlightthickness=0,
command=lambda: print("Export pressed"),
relief="flat"
)
btn_export.place(
x=800.0,
y=716.0,
width=200.0,
height=58.0
)
btn_image_pathpicker = PhotoImage(
file=gui_settings.assets("btn_pathpicker.png"))
btn_pathpicker = Button(
image=btn_image_pathpicker,
borderwidth=0,
highlightthickness=0,
command=lambda: open_textfile(textbox_text),
relief="flat"
)
btn_pathpicker.place(
x=50.0,
y=716.0,
width=65.0,
height=58.0
)
btn_image_back = tkinter.PhotoImage(
file=gui_settings.assets("btn_back.png"))
btn_back = Button(
image=btn_image_back,
command=lambda: self.closewindow()
)
btn_back.place(
x=0.0,
y=0.0,
width=65.0,
height=58.0
)
canvas.create_text(
207.0,
17.0,
anchor="nw",
text="Insert Text:",
fill="#222831",
font=("Roboto", 36 * -1)
)
textbox_text = tkinter.Text(
bd=0,
fg='#EEEEEE',
bg="#00ADB5",
highlightthickness=0
)
textbox_text.place(
x=50.0,
y=79.0,
width=500.0,
height=602.0
)
canvas.create_text(
752.0,
17.0,
anchor="nw",
text="Summarized Text:",
fill="#EEEEEE",
font=("Roboto", 36 * -1)
)
textbox_summarized = tkinter.Text(
bd=0,
fg='#222831',
bg="#EEEEEE",
highlightthickness=0
)
textbox_summarized.place(
x=650.0,
y=79.0,
width=500.0,
height=602.0
)
self.resizable(False, False)
def closewindow(self):
self.destroy()
Ich hoffe man kann hier weiterhelfen, dass ich über MainForm die Summarization Form aus einer anderen Python Datei aufrufen kann. Viele Tutorials, die ich mir angesehen habe, arbeiten mit der GUI aus einer Datei heraus und konnten mein Problem nicht wirklich lösen.