Переключение между двумя кадрами в отдельных файлах в tkinterPython

Программы на Python
Гость
Переключение между двумя кадрами в отдельных файлах в tkinter

Сообщение Гость »


I am writing a program where I have the opportunity, to switch between two frames which are one two separate Files. The main file is called "Main_Menu" while the other file is called "first_page". When I import the classes from both classes into each other, then I get an circular import which leads to the program not working. Does anybody have a solution? The codes to both files are under this Text.

Main_Menu.py

import tkinter as tk from tkinter import PhotoImage from second_page import SecondPage from first_page import FirstPage class Hauptfenster: def init(self, root): self.root = root self.root.title("Hauptfenster") # Picture load self.background_image = PhotoImage(file="Background_Main.png") # Size of the image self.width = self.background_image.width() self.height = self.background_image.height() # Create main frame self.main_frame = tk.Frame(self.root, width=self.width, height=self.height) self.main_frame.pack_propagate(False) # Verhindert, dass der Rahmen seine Größe an die Widgets anpasst self.main_frame.pack() # Packe den Haupt frame, um ihn sichtbar zu machen # Add background image self.background_label = tk.Label(self.main_frame, image=self.background_image) self.background_label.place(x=0, y=0, relwidth=1, relheight=1) # If the picture fills the frame # Rahmen für den ersten Knopf erstellen self.create_character_frame = tk.Frame(self.main_frame, bg="white", bd=0, relief="flat") self.create_character_frame.place(relx=0.4, rely=0.7, anchor="center") # Create frame for the second button self.Archiv_frame = tk.Frame(self.main_frame, bg="white", bd=0, relief="flat") self.Archiv_frame.place(relx=0.6, rely=0.7, anchor="center") # Frame for the close button self.close_frame = tk.Frame(self.main_frame, bg="white", bd=0, relief="flat") self.close_frame.place(relx=0.5, rely=0.9, anchor="center") # Frame for the logo self.logo_frame = tk.Frame(self.main_frame, bg="white", bd=0, relief="flat") self.logo_frame.place(relx=0.5, rely=0.2, anchor="center") # Add text for the first button self.create_character_image = PhotoImage(file="character_text.png") self.create_character_button = tk.Button(self.create_character_frame, image=self.create_character_image, relief="flat", command=self.zeige_erste_seite) self.create_character_button.pack(pady=10) # Add image for the first button self.first_image = PhotoImage(file="Bild_eins.png") self.first_image_label = tk.Label(self.main_frame, image=self.first_image, bd=0, highlightthickness=0) self.first_image_label.place(relx=0.4, rely=0.511, anchor="center") # Add text for the second button self.Archiv_image = PhotoImage(file="Archiv_Text.png") # Beispielbild, ersetze es mit deinem Bild self.Archiv_button = tk.Button(self.Archiv_frame, image=self.Archiv_image, relief="flat", command=self.zeige_zweite_seite) self.Archiv_button.pack(pady=10) # Add image for the second button self.Sheet_image = PhotoImage(file="Bild_zwei.png") self.Sheet_image_label = tk.Label(self.main_frame, image=self.Sheet_image, bd=0, highlightthickness=0) self.Sheet_image_label.place(relx=0.6, rely=0.511, anchor="center") # Add button to close the program self.close_image = PhotoImage(file="Escape.png") self.close_button = tk.Button(self.close_frame, image=self.close_image, command=root.destroy, relief="flat") self.close_button.pack(pady=10) # Insert logo self.logo_image = PhotoImage(file="logo.png") self.logo_image_label = tk.Label(self.main_frame, image=self.logo_image, bd=0, highlightthickness=0) self.logo_image_label.place(relx=0.5, rely=0.2, anchor="center") def zeige_erste_seite(self): # Alles im Haupt frame löschen for widget in self.main_frame.winfo_children(): widget.destroy() # Create an object of the class for the first page FirstPage(self.main_frame) def zeige_zweite_seite(self): # Alles im Haupt frame löschen for widget in self.main_frame.winfo_children(): widget.destroy() # Create an object of the class for the second page SecondPage(self.main_frame) def main(): root = tk.Tk() app = Hauptfenster(root) root.mainloop() if name == "main": main() first_page.py

import tkinter as tk from Main_Menu import Hauptfenster class FirstPage(tk.Frame): def init(self, parent): self.parent = parent self.frame = tk.Frame(self.parent) self.frame.pack(expand=True, fill='both') # Add new content for the second page label = tk.Label(self.frame, text="Das ist die erste Seite") label.pack() # Add button to the main menu self.back_to_main_button = tk.Button(self.frame, text="Zurück zum Hauptmenü", command=self.back_to_main) self.back_to_main_button.pack() def back_to_main(self): # Delete everything in the frame for widget in self.frame.winfo_children(): widget.destroy() # Create an object of the class for the main menu Hauptfenster(self.parent) I wanted to switch between those two while they are on separate files.

Вернуться в «Python»