在Tkinter GUI中点击按钮运行其他Python脚本



我有多个具有不同功能的python脚本。我想创建一个GUI结合我的其他脚本的所有功能:像从网络下载文件,通过运行一个脚本,如果一个按钮"下载";在我的GUI上点击。

这是我目前的代码,(我从互联网上尝试了一些代码,但我找不到一个彻底的例子或解决方案):

# Import modules
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo

# Display settings
root = tk.Tk()  #Create application window

# Display settings
window_width = 600    #Set window height and width
window_height = 500
screen_width  = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
center_x = int(screen_width/2 - window_width/2)
center_y = int(screen_height/2 - window_height/2)
root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
root.resizable(0, 0)
root.attributes('-topmost', 1)
root.attributes('-alpha', 1)     #Adjust transparacy
root.iconbitmap(r'my_URL')
root.title("Client Data Processing") # Create window title

# Download button
download_icon = tk.PhotoImage(file=r"my_URL")
download_button = ttk.Button(root, image=download_icon,text="Download", compound=tk.LEFT)
download_button.pack(ipadx=5,ipady=5,expand=True)

# Exit button
exit_button = ttk.Button(root,text="Exit",command=lambda: root.destroy())
exit_button.pack(ipadx=5,ipady=5,expand=True)
# Keep GUI on display
root.mainloop()

我添加了以下内容,并"my_script"在GUI打开之前运行。我只需要"my_script"当我点击按钮时运行,我试过my_script.main()。main()会工作吗?我在一些例子中见过它(我不打算在下面添加main())。

import my_script
script = my_script
download_button = ttk.Button(root, image=download_icon,text="Download Outlook Attachments", compound=tk.LEFT, command=script)

你能做的就是导入其他/其他脚本,并在点击

按钮时运行它们应该是这样的

# Import modules
from YourScript import myFunc #this will import the function that you want from another script
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo

# Display settings
root = tk.Tk()  #Create application window

# Display settings
window_width = 600    #Set window height and width
window_height = 500
screen_width  = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
center_x = int(screen_width/2 - window_width/2)
center_y = int(screen_height/2 - window_height/2)
root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
root.resizable(0, 0)
root.attributes('-topmost', 1)
root.attributes('-alpha', 1)     #Adjust transparacy
root.iconbitmap(r'my_URL')
root.title("Client Data Processing") # Create window title

# Download button
download_icon = tk.PhotoImage(file=r"my_URL")
download_button = ttk.Button(root, image=download_icon,text="Download", compound=tk.LEFT, command=lambda:myFunc) #command property tells tkinter what you wanna execute when button is clicked
download_button.pack(ipadx=5,ipady=5,expand=True)

# Exit button
exit_button = ttk.Button(root,text="Exit",command=lambda: root.destroy())
exit_button.pack(ipadx=5,ipady=5,expand=True)
# Keep GUI on display
root.mainloop()

这样你就可以调用其他脚本,并在点击按钮时运行导入的函数。

也可以使用

#import the entire script
import YourScript
#use a function inside the script
download_button = ttk.Button(root, image=download_icon,text="Download", compound=tk.LEFT, command=lambda:YourScript.myFunc) 

相关内容

  • 没有找到相关文章

最新更新