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()
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)
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)
Related
actually what my code does is, asking for some information and then when he/she finishes,I will move her to the next page, which asks her for Email and password:
import tkinter as tk
from tkinter import ttk
class Bio():
def __init__(self):
self.window = tk.Tk()
self.frame = tk.Frame(self.window)
self.window.title('Data form')
self.frame.pack()
def main(self):
Bio.personaly(self)
Bio.information(self)
Bio.beruflich(self)
Bio.term_accept(self)
Bio.enter(self)
Bio.retrive(self)
self.window.mainloop() # Runs 'til i quit
well, The rest of the code is just operations
what i except is, When the user finishes and clicks the button (i have it in my code), the window is closed and a new window is opened
my question is how can i close the previous window and where can i start and open the new window?
You can do it like this withe the destroy statement:
import tkinter as Tk
from tkinter import *
root = Tk()
width= root.winfo_screenwidth()
height= root.winfo_screenheight()
root.geometry("%dx%d" % (width, height))
root.title("selfdestruction")
root.config(bg="black")
def exit():
root.destroy()
destroy_button = Button(root, text="exit",command=lambda: exit())
destroy_button.place(relx=0.5, rely=0.5, anchor="center")
root.mainloop()
i want to disable buttons when i call subprocess to execute another tkinter window and to prevent multi-showing windows when i click too many times on buttons, then after finishing from the second calling window i want to re-enable them.
menubar.entryconfig("tester", state="disabled") and
menubar.entryconfig("tester", state="normal")
both of them in the same function does not ndo the trick. is there any other way to do it. And thanks in advance.
# add the necessairy librairy
import tkinter as tk
import subprocess
def Region_windows2():
menubar.entryconfig("tester", state="disabled")
menubar.entryconfig("tester2", state="disabled")
menubar.entryconfig("tester3", state="disabled")
subprocess.call(['/home/pi/test3/script.sh'])
menubar.entryconfig("tester", state="normal")
# here's the main window
Mafenetre = tk.Tk()
#set main window title
Mafenetre.title("GUI")
Mafenetre['bg']='white' # couleur de fond
# get screen width and height
wf1= Mafenetre.winfo_screenwidth()
hf1= Mafenetre.winfo_screenheight()
A = str(wf1)
B = str(hf1)
# set the dimensions of the screen
# and where it is placed
Mafenetre.geometry(A+"x"+B)
Mafenetre.attributes('-fullscreen', True)
# add menubar to the main window
menubar = tk.Menu(Mafenetre, bg='#1f1b13', fg='white')
# menubar button to test the second window
menubar.add_command(label="tester", command=Region_windows2)
menubar.add_command(label="tester2", command=hello)
menubar.add_command(label="tester3", command=hello)
# add menubr to the main window
Mafenetre.config(menu=menubar)
pwd = tk.Entry(Mafenetre, show="*")
pwd.pack()
pwd.focus()
Mafenetre.mainloop()
Im developing a audio player for python. Unfortunately, it appears as if after a modification, the file dialog portion of the script ceased working. The code below is a minimum reproducible example of the offending bit.
# -----------------------Setup-----------------------
# Imports
from tkinter import *
from tkinter.ttk import *
from tkinter import filedialog
import simpleaudio
import subprocess
# GUI
root = Tk()
root.title("CupPlayer")
menubar = Menu(root)
file = Menu(menubar, tearoff = 0)
root.config(menu = menubar)
# ----------------------Functions---------------------
def file_selector():
filename = filedialog.askopenfilename()
root.update()
if filename.endswith('.mp3'):
subprocess.call(['ffmpeg', '-i', filename,
'audio.wav'])
btn = Button(root, text = 'search file !', bd = '5',
command = file_selector)
mainloop()
Hey #RandomInternetPerson first of all dont use subprocess.call inside a ui loop at all its make freeze and crashing the ui it will wait until subprocess process is open so change it to subprocess.Popen and check if it fixed
I am following a Tkinter tutorial and I have a very basic GUI built. When I run it, the GUI opens and everything is fine, except the button is not immediately clickable. The only way I can get it to click is if I switch to a full screen window and back to the GUI. Below is the code I am using, along with a screenshot. Does anyone know why this is happening? If it does not make enough sense I can upload a video. Thank you.
import tkinter as tk
from tkinter import Frame
HEIGHT = 700
WIDTH = 800
root = tk.Tk()
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
frame = tk.Frame(root, bg='#ff9a7f')
frame.place(relwidth=2, relheight=1)
button = tk.Button(root, text='Test Button')
button.pack()
root.mainloop()
I'm trying to create a python program tkinter that, upon the pressing of a button, opens a new full screen tkinter window containing an image and plays an audio file - here's my code:
from tkinter import *
from PIL import Image, ImageTk
from playsound import playsound
def play():
window = Toplevel()
window.attributes('-fullscreen', True)
img = ImageTk.PhotoImage(Image.open("pic.png"))
label = Label(window, image=img).pack()
playsound("song.mp3")
buttonWindow = Tk()
b = Button(buttonWindow, text="Press Button", command=play)
b.pack()
(my image and audio file are both on the desktop with my python file)
However, when I run my code, when I press the button, the audio plays but no second tkinter window opens.
I've tried to destroy() the buttonWindow and have tried many different ways of including an image on a tkinter window - if I remove the line of code using PhotoImage(), the window appears (obviously I then get a syntax error stating that 'img' is not defined).
How could I solve this?
Thanks,
Louis
I had similar problem.
But i resolve this trial and error method.
First of all i don't use pillow library and put image into Label
You should try define Photoimage object NOT IN FUNCTION
Example:
It will work:
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
def popUp():
popff = tk.Toplevel(root)
popff.geometry("400x200")
labelImgff = tk.Label(popff, image=imgff, bg="white")
labelImgff.grid(row=0, column=0)
imgff = tk.PhotoImage(file="/home/user/image.png")
btnff = tk.Button(text="startPOP", command=popUp)
btnff.grid(column=0,row=0)
root.mainloop()
effect of action
Your playsound() command is blocking execution. The playsound() command has an optional field 'block', which is True by default. Changing this to False will continue execution and allow mainloop() to continue.
Second, just call label.draw() to draw your image to the TopLevel window.
Here's the code:
from tkinter import *
from PIL import Image, ImageTk
from playsound import playsound
def play():
window = Toplevel()
window.attributes('-fullscreen', True)
img = ImageTk.PhotoImage(Image.open("pic.jpeg"))
label = Label(window, image=img).pack()
playsound("song.mp3",block=False)
label.draw()
buttonWindow = Tk()
b = Button(buttonWindow, text="Press Button", command=play)
b.pack()
buttonWindow.mainloop()
Cheers!