I have multiple python scripts with different functionalities. I want to create a GUI incorporating all functions of my other scripts: Like download a file from the web, by running a script, if a button "Download" On my GUI is clicked.
This is my current code, (I have tried some code from the internet, but I can't find a thorough example or solution):
# 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()
CodePudding user response:
What you can do is import your other/others scripts and run them when you click the button
It should look like this
# 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=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()
This way you call your other script and run the function imported when the button is clicked.
You can also use
#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=YourScript.myFunc)
CodePudding user response:
I have added the following, and "my_script" runs before the GUI opens. I just want "my_script" to run when I click the button, I have tried my_script.main(). Will main() work? I have seen it in some examples (I intend not to have added main() belowe).
import my_script
script = my_script
download_button = ttk.Button(root, image=download_icon,text="Download Outlook Attachments", compound=tk.LEFT, command=script)