[英]Importing all needed modules in one main file that has all needed libraries for modules imported
例如,我有2個文件,mother.py和child.py,child.py是在mother.py中導入的模塊
mother.py中的代碼是:
from tkinter import *
from tkinter import ttk
from modules.child import LoginWindow
root = Tk()
window = LoginWindow(root)
root.mainloop()
child.py中的代碼是:
class LoginWindow:
def __init__(self, master):
self.master = master
self.content = ttk.Frame(self.master, padding=(20,30,20,30))
當我這樣做時,它給我一個錯誤,提示tkk
(在tkk
的最后一行),但在mother.py(第2行)中定義了為什么不能正常工作以及什么是最好的方法做這樣的事情
用Python導入不同於其他語言中的“包含”。 整個模塊包含在以您導入的模塊命名的對象內。 因此,當您這樣做時:
from modules.child import LoginWindow
整個模塊包含在對象/變量LoginWindow
。 在這種情況下,“子”模塊無法看到導入該模塊的模塊中定義了哪些變量。
在問題的示例中,您想移動:
from tkinter import ttk
到child.py
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.