I'm building a GUI app with tkinter that based on a button it will run another py file that is in the same folder as this GUI. When i'm running the app in my working environment, everything is working fine. When i'm creating an executable app and pressing the buttons, for a second i see a black window being open and then it's being closed and the function that the i assigned the button too is continuing to the next line (a messagebox to say when the script is done running) without running the py file. I'm running the .py files by using os.system('python filename.py') and not sure if that is what causing my problem. Would appreciate any help!
Edit: I saw that for some reason it says that my json file to use for google sheets is looking in the output folder where the auto-py-to-exe is directing it. i added the json file in the bundle but it still looking for it in the wrong place.
Here are a few functions that are assigned to a button click:
# This function will run the python script to create new and styled worksheet.
def run_new_worksheet_code():
return os.system("python script_to_create_automated_sheets_each_week.py")
# This function will let the code run without freezing the GUI and creating the loading bar
# once you pressed the "Create Worksheet" button.
def new_thread_for_wks_code():
global submit_thread
global progress_bar
submit_thread = threading.Thread(target=run_new_worksheet_code)
submit_thread.daemon = True
submit_thread.start()
team_lead_window.after(20, check_when_wks_is_ready)
style = ttk.Style()
style.configure("TProgressbar", background='#30D5C8', troughcolor='#DBDBDB',
bordercolor='#DBDBDB', lightcolor='#30D5C8', darkcolor='#30D5C8')
# Creating a progress bar to indicate that the hours are loading into the google sheets.
progress_bar = ttk.Progressbar(team_lead_window, orient = 'horizontal',
length =300, mode = 'determinate')
progress_bar.place(relx=0.08, rely=0.6, width=250, height=15)
# This function will check every 20 ms if the worksheet is still being prepared
# and when it will finish - a pop-up message will say it's done!
def check_when_wks_is_ready():
if submit_thread.is_alive():
team_lead_window.after(20, check_when_wks_is_ready)
loading_bar_for_wks_update()
else:
progress_bar.destroy()
tk.messagebox.showinfo('New Worksheet', 'New Worksheet Has Created Succsefully!')
Related
I have a .py file that runs via Task Scheduler when I log into my computer, and it's constantly listening for specific keyboard events to happen. Rather than having it run in the Python Console window on the Taskbar, I'd like to get it to run in the system tray with a Python icon to make it more clean.
I have discovered the pysystray library, and can successfully make a Python icon in the system tray with a right-click menu which will allow me to quit the icon.
from os import path as path
import PIL.Image
import pystray
def on_clicked(icon, item):
if str(item) == "Quit":
icon.stop()
dir_path = path.dirname(path.realpath(__file__))
icon_image = "\\Icon\\Python.ico"
full_path = dir_path + icon_image
image = PIL.Image.open(full_path)
icon = pystray.Icon(
"Testing",
image,
menu=pystray.Menu(pystray.MenuItem("Quit", on_clicked)),
)
icon.run()
However, I can't figure out how to get my existing Python file to run under the icon. It seems like there are two separate events happening; one for launching the icon, and another for running the rest of the code. I would want to make sure that if the "Quit" option is chosen, then the .py file would also stop.
Also, it would be really helpful to have a separate right-click menu item which will display either Python cmd line text since it's constantly being overwritten in my main .py file with a print statement.
Any help/insight would be really appreciated!
The pystray.run function accepts an optional callback function as a parameter. The callback function is run in a separate thread and should be closed normally by ln 9: icon.stop().
I have done a script that process some images and at the end, it asks you where do you want to save the file. I made it using tkinter (code below). It works fine, the only problem that I have is:
I have an exit button linked with the function destroy and when I click it, the root is closed, but the script is still running, no error appear and I have to stop it manually. If I use quit the script is stopping but the root window freezes and kernel error appear restarting python. I tried to use sys.exit() after the root.mainloop but the script is not going out of the loop. Hope you can help, thank you.
I am using Spider, python 3.7.6, Ipython 7.19.0
root = Tk()
root.geometry('200x150')
# function to call when user press
# the save button, a filedialog will
# open and ask to save file
def save():
filename2 = filedialog.asksaveasfile(mode='wb+',defaultextension=".tif", title="Choose filename")
if not filename2:
return
imwrite(filename2, stack, imagej=True)#
# root.destroy()
button_save = Button(root, text = 'Save', command = lambda : save())
button_save.grid(row=1,column=2)
button_exit=Button(root,text='Exit program', command=root.destroy)
button_exit.grid(row=5,column=2)
root.mainloop()
Ok, I solved it, perhaps it helps someone in the future.
I defined a new function to quit as follows:
def _quit():
root.quit()
root.destroy()
button_exit=Button(root,text='Exit program', command=_quit)
I’m building a program in python that will copy large files using robocopy. Since the gui freezes while the copy is done i only have two options:
1. Learn how to do multithreading and design the gui to show the progress and not freeze.
2. Keep the console on after compiling with pyinstaller as an alternative to show robocopy progress while the gui freezes.
I am open to doing multithreading but i’m a beginner and is pretty hard to understand how to make another subprocess for robocopy and from there extract the progress into a label from gui. The option i thought about is to have the cmd console active only while the copy is done. Is it possible? The scenario will be like this:
Open the program (the console will be hidden)
Press the copy button (console pops up and shows the copy progress while the gui freezes)
After the copy is done hide the console again
As i said above. I’m not totally excluding adding multithreading but for that i will need some help.
Thanks!
Please try this code, should be working, please let me know if something wrong:
import tkinter as tk
import os
import subprocess
import threading
main = tk.Tk()
main.title('Title')
frame_main = tk.Frame(main)
frame_main.grid(columnspan=1)
src = 'D:/path/to/the/folder'
dest = 'D:/path/to/the/folder2'
selection_platf = len(os.name)
def copy_build_button():
if selection_platf < 11:
subprocess.call(["robocopy", src, dest, r"/XF", 'BT V_SyncPackage.zip', "/S"])
else: #for linux
subprocess.call(["robocopy", src, dest, "/S"])
def copy_thread():
thread_1 = threading.Thread(target=copy_build_button)
thread_1.start()
button_main1 = tk.Button(frame_main, text="copy_build_button", width=50, height=5, fg="green", command=copy_thread)
button_main1.grid(column=0, sticky='N'+'S'+'E'+'W')
main.mainloop()
I am trying to write a Bash script so that I can run my program on a double click. The program uses tkinter and the GUI is the only thing I need to see. My bat file is the following:
python BudgetGUI.py &
This runs the code and successfully prints any print statements I have throughout my code but it never opens up the GUI. It simply runs through and closes immediately.
How can I modify the bash script to run the GUI?
Thanks in advance!
Edit Solutions for both mac and pc would be great, though at the moment I am on PC. I am working in Python3.
You need to add a call to mainloop(). I can't say for sure without seeing your code, but probably you need to add root.mainloop() to the bottom.
You don't need a bash or bat file. For your mac, just add a shebang and make the file executable. For Windows, add a shebang and associate the file with py.exe.
If you want to suppress the command line from popping up along with the GUI, rename your file with a .pyw extension.
Make the window made with tkinter stay out of IDLE*
if you have
root = Tk()
at the end put
root.mainloop()
if you have another name for that like:
window1 = Tk()
then, at the end put
window1.mainloop()
and so for any name you gave to the istance of Tk()
A little example
import tkinter as tk
class Window:
root = tk.Tk()
label = tk.Label(root, text = "Ciao").pack()
app = Window()
app.root.mainloop()
Python 2
The code above is for python 3.
For python 2 you just need to change the first line (Tkinter with T, not t)
import Tkinter as tk
I'm making app that uploads video to server, so I've got an START.py file which makes Tkinter window and handles all Tkinter widgets and another one (which is being run by START.py file), upload_video_to_server.py, which obviously, uploads video to server. I'm doing this using Threading so app doesn't freeze while it's uploading, so I want to make active ttk.ProgressBar which will track upload. I managed to get needed informations (file size and already uploaded size) from upload_video_to_server.py but problem is that ProgressBar is inside START.py. After googling and many tries, I saw that cyclic imports ask for importing inside function for various reasons, so I did that like this:
START.py
def chProgressBarValue(value):
progress().prog_bar["value"] = value
print "ProgBarValueChanged to: ", prog_bar["value"]
def chProgressBarMax(max):
progress().prog_bar["maximum"] = max
def progress():
global prog_bar
print "prog bar is being made"
prog_bar = ttk.Progressbar(
app, orient="horizontal",
length=200, mode="determinate",
value=5,
max=15
)
prog_bar.pack(side=TOP)
prog_bar.place(x=380, y=395)
upload_video_to_server.py
def printTotals(transferred, toBeTransferred):
print "Transferred: {0}\tOut of: {1}".format(transferred, toBeTransferred)
import START
START.chProgressBarMax(toBeTransferred)
START.chProgressBarValue(transferred)
But after first print of transferred data information, new Tkinter window shows up and freezes/crashes whole app (uploading works like a charm without importing START.py). Here is a pic of log + problem (second empty Tkinter windows SHOULDN'T be made!)