简体   繁体   中英

How to call modules from another .py file

first time posting!

I'm fairly new to python (and programing,). and I have started to make a basic tkinter program, The code is getting pretty long now. and I want to split it between different.py files to make it more navigatable, So far, all my code exists in classes, seperating the main window, from calculation functions. secondary windows and so on.

First question, is it considered good practice to split code like this? I feel like it is, but want to make sure!

Secondly, how is the best way to handle modules between files?

For example, I have tkinter and matplotlib imported in the main_window.py file. I have the main_window class function which calls a different class which I want to move to another file, but this secondary class has a line which calls tkinter. I want to pass self through the secondary function so that it is using the same instance, as it were.

Here is an example code to illustrate. The first file, main_window.py:

# main_window.py    

import tkinter as tk
import matplotlib
import matplotlib.pyplot as plt
import app_design  # app_design.py contains the code I want to break out

class MainWindow:
        def __intit__(self, root):
                self.root = root
                self.start_gui()
    
        def start_gui(self):
                # a bunch of code
                ...
                # in reality this is read from a program file on startup                    
                color_mode = "Dark"

                # This is just an example, the matplotlib call in the other app is in the __init__                    
                self.bg_col_mode = tk.StringVar()

                self.bg_col_mode.set(app_design.AppColors(color_mode).background())

                # a bucnh more code of various tk widgets and matplotlib charts
                ...


if __name__ == '__main__':
        app = MainWindow(tk.Tk())
        app.root.mainloop()

and then an example of some code which I'd like to split out. This is not the only instance of the class referencing a module outside the MainWindow class, but it works as an example:

# app_design.py

class AppColors:
        def __init__(self, color_mode):
                self.color_mode = color_mode

                if self.col_mode == "Dark":
                        self.plt.style.use("Dark")  # it is this .plt call that has moved from the main_window.py file to the new file
                else:
                        self.plt.style.use("Light")

        def background_mode(self):
                if self.color_mode == "Dark":
                        return "#292929"  # colour code
                else:
                        return "#F8F1F1"

Hopefully this makes sense!

First question, is it considered good practice to split code like this? I feel like it is, but want to make sure!

I actually don't know myself, I only have coded stuff for backend.

Secondly, how is the best way to handle modules between files?

You simply import the file (or function directly).

Example:

file1.py

def hello(name):
    print("Hello ", name)

file2.py

from file1 import hello

hello("arjix")

this way you can directly use the function hello

or

import .file1

file1.hello("arjix")

PS: Make sure these two files are in the same folder.

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