如何在Python中用一个按钮链接多个脚本?

问题描述 投票:0回答:1

我正在用 Python

Tkinter
编写一个程序,并且我有一个
main.py
studio.py

我想连接它们。

main.py
是菜单,它有一个按钮可以进入工作室。

所以我想从

main.py
导航到
studio.py

有人可以帮助我吗?

我尝试了找到的所有指南,但没有一个有效。

python tkinter
1个回答
0
投票

很高兴回答您的问题,希望您度过愉快的一天。 :D

我希望我理解你的问题,在这种情况下,你需要将脚本结构导入到主脚本中,并使用主脚本中的方法和类。您可以在主脚本中导入辅助脚本,例如:

#Main script

import theSecondaryScript

示例

这是关于在 Python Tkinter 中连接 main.py 和 studio.py 的扩展说明:

1。构建您的代码:

  • main.py:
    • 创建主窗口 (
      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.py:
    • 创建工作室窗口 (
      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。导入必要的模块:

  • 在main.py中,导入studio模块:
import studio

3.创建按钮并将其链接到函数:

  • 在 main.py 中,创建按钮并将其绑定到
    open_studio
    函数:
button = tk.Button(main_window, text="Go to Studio", command=open_studio)
button.pack()

4。定义

open_studio
函数:

  • 在main.py中,定义打开studio窗口的函数:
def open_studio():
    studio.create_studio_window()  # Call the studio window function

5。创建工作室窗口:

  • 在studio.py中,定义创建studio窗口的函数:
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

© www.soinside.com 2019 - 2024. All rights reserved.