2

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()
2
  • I have the solution! I went and added a function "extract" in the mean time. I forgot the "command=lambda:" option. ''' download_button = ttk.Button(root, image=download_icon,text="Download Outlook Attachments", compound=tk.LEFT, command=lambda: extract([""], 31)) ''' Commented Jan 23, 2023 at 14:04
  • Edit into your question not in comment and answer. Commented Jun 11, 2023 at 19:57

2 Answers 2

0

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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=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()

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=lambda:YourScript.myFunc) 

2 Comments

My entire script runs before I have clicked the button. Should this happen?
replace command=... for command=lambda:yourfunc. Answer edited

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.