简体   繁体   中英

how to Divide class based frames in tkinter frames into different python files

I have a tkinter application which is divide into many classes(frames in tkinter) the program has become 1000+ lines and now I want to divide it into deferent files can any one guide me how can I achieve this.

my code is like

class tkinterApp(tk.Tk):  #this is the class which make navigation pages possible 
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)

        self.frames = {}
        self.attributes('-fullscreen', True)

        for F in (StartPage, connect_page, login_page, signup_page, dashboard_page,trip_page,destination_reached_page):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        #some code .....

class connect_page(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        #some page...

#more classes like this ........

I want to divide the pages into different python files and and main class tkinterApp in different file is this possible if How?

Just cut / paste the class and all the imports it needs into another file, then import it.

# start_page.py

import tkinter as tk

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        #some code .....

and then in your main file:

#!/usr/bin/env python3

# main_program.py

from start_page import StartPage

# rest of your code

As a side note, 1000+ lines is not that large and not uncommon. You may want to read up on your IDE's code navigation features.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM