我正在用 Python
Tkinter
编写一个程序,并且我有一个 main.py
和 studio.py
。
我想连接它们。
main.py
是菜单,它有一个按钮可以进入工作室。
所以我想从
main.py
导航到 studio.py
。
有人可以帮助我吗?
我尝试了找到的所有指南,但没有一个有效。
很高兴回答您的问题,希望您度过愉快的一天。 :D
我希望我理解你的问题,在这种情况下,你需要将脚本结构导入到主脚本中,并使用主脚本中的方法和类。您可以在主脚本中导入辅助脚本,例如:
#Main script
import theSecondaryScript
这是关于在 Python Tkinter 中连接 main.py 和 studio.py 的扩展说明:
1。构建您的代码:
main_window
) 和菜单元素。open_studio
)来处理将打开工作室窗口的按钮单击。import tkinter as tk
import studio # Import the studio module
class MainWindow:
def __init__(self):
self.main_window = tk.Tk() # Create the main Tkinter window
self.create_menu()
self.create_button()
def create_menu(self):
# Add your menu elements here
pass
def create_button(self):
button = tk.Button(self.main_window, text="Go to Studio", command=self.open_studio)
button.pack()
def open_studio(self):
studio.create_studio_window() # Call the studio window function
if __name__ == "__main__":
main_app = MainWindow()
main_app.main_window.mainloop()
studio_window
) 及其内容。import tkinter as tk
def create_studio_window():
studio_window = tk.Tk() # Create a new Tkinter window for the studio
# Add your studio window elements here
studio_window.mainloop() # Start the studio window's event loop
2。导入必要的模块:
import studio
3.创建按钮并将其链接到函数:
open_studio
函数:button = tk.Button(main_window, text="Go to Studio", command=open_studio)
button.pack()
4。定义
open_studio
函数:
def open_studio():
studio.create_studio_window() # Call the studio window function
5。创建工作室窗口:
def create_studio_window():
studio_window = tk.Tk() # Create a new Tkinter window for the studio
# Add your studio window elements here
studio_window.mainloop() # Start the studio window's event loop
main.py
import tkinter as tk
import studio
class MainWindow:
def __init__(self):
self.main_window = tk.Tk() # Create the main Tkinter window
self.create_menu()
self.create_button()
def create_menu(self):
# Add your menu elements here
pass
def create_button(self):
button = tk.Button(self.main_window, text="Go to Studio", command=self.open_studio)
button.pack()
def open_studio(self):
studio.create_studio_window() # Call the studio window function
if __name__ == "__main__":
main_app = MainWindow()
main_app.main_window.mainloop()
工作室.py
import tkinter as tk
def create_studio_window():
studio_window = tk.Tk() # Create a new Tkinter window for the studio
# Add your studio window elements here
studio_window.mainloop() # Start the studio window's event loop
import
访问其他模块的代码。command
选项指定单击时调用哪个函数。tk.Tk()
为工作室创建一个新的 Tkinter 窗口。mainloop()
以启动其事件循环。如果您想查看使用 python 链接多个脚本的真实示例,您可以查看以下示例:https://github.com/Gato-Capitao/LoomyPad/blob/main/LoomyPad.py