简体   繁体   English

如何从 another.py 文件中调用模块

[英]How to call modules from another .py file

first time posting!第一次发帖!

I'm fairly new to python (and programing,).我对 python (和编程)相当陌生。 and I have started to make a basic tkinter program, The code is getting pretty long now.我已经开始制作一个基本的 tkinter 程序,代码现在变得很长。 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.我想在不同的.py文件之间拆分它以使其更易于导航,到目前为止,我所有的代码都存在于类中,将主要的window与计算函数分开。 secondary windows and so on.二级windows等。

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.例如,我在 main_window.py 文件中导入了 tkinter 和 matplotlib。 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 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.我想通过辅助 function 传递 self ,以便它使用相同的实例。

Here is an example code to illustrate.这是一个示例代码来说明。 The first file, main_window.py:第一个文件 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:这不是 class 引用 MainWindow class 外部模块的唯一实例,但它可以作为示例:

# 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).您只需导入文件(或直接导入 function)。

Example:例子:

file1.py文件1.py

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

file2.py文件2.py

from file1 import hello

hello("arjix")

this way you can directly use the function hello这样就可以直接使用 function 你好

or或者

import .file1

file1.hello("arjix")

PS: Make sure these two files are in the same folder. PS:确保这两个文件在同一个文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从新 window 中的 .py 文件调用另一个 python 脚本 - How to call another python script from a .py file in a new window 如何从python中的另一个.py文件调用函数? - How to call function from another .py file in python? 如何从另一个py文件调用函数到django中的模型类 - How to call a function from another py file to a model class in django 如何从another.py调用Python代码? (当我按空格键时 both.py 在同一个文件中) - How to call Python code from another .py? (when I press space bar both .py are in the same file) 如何设置循环以从其他程序包执行模块(* .py)? - How to set a loop to execute modules(*.py) from another packages? 如何将功能从.py文件导入到另一个.py文件? - How to import functions from a .py file to another .py file? 来自另一个py文件的maya python调用函数 - maya python call function from another py file 如何从另一个 py 文件导入一个类? - How to import a Class from another py file? 使用用户输入从另一个.py文件中调用在不同.py文件中定义的特定函数 - Call a specific function defined in different .py file from another .py file using user input 如何调用另一个 python 文件,运行它,并将 main.py 替换为 new.py 文件? - How to call another python file, run it, and replace main.py with the new .py file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM