Restart a program in Tkinter using a button - python

I'm new to Python and are trying to do a small program in which the GUI-window is supposed to re-open while clicking the button.
I read online that the line of code
os.execl(sys.executable, sys.executable, *sys.argv)
is supposed to make your program restart, but all it does is exit the GUI-window and restart Shell totally clean.
from tkinter import *
import sys
import os
def restart_program():
os.execl(sys.executable, sys.executable, *sys.argv)
window = Tk()
window.title("Test")
button = Button(window, text="RESTART", width=6, command=restart_program).grid(row=5, column=0)
window.mainloop()
This is the relevant part of my code atm (I didn't include all the code - some more widgets are adding to the existing, but it's not relevant to the problem). Is there any way to re-open the window called "Test", especially without having to put it in a function? Would be very thankful for help! :)))

Related

How to unite 2 scripts in one using tkinter?

I'm making an tkinter app. I have 2 scripts and I have to make an GUI where you can press one button and scripts will be run in sequence. Also I should note that my scripts also use tkinter.
I tried to make it just by making 2 functions for each script and putting them in one button, but it doesn't work right. It's only open window of first script and second one starts only if I close both open windows.
This is what i tried
import tkinter as tk
from tkinter import ttk
def run_script_1():
# Code for running script 1
print("Running script 1")
def run_script_2():
# Code for running script 2
print("Running script 2")
def run_both_scripts():
run_script_1()
run_script_2()
root = tk.Tk()
root.title("Script Runner")
root.geometry('600x100')
frame = ttk.Frame(root)
frame.pack()
button = ttk.Button(frame, text="Run Both Scripts", command=run_both_scripts)
button.pack()
root.mainloop()

Tkinter not working the first time it runs with withdraw()

I am trying to make a Tkinter script to select files through Windows File Explorer. I don't need the Tkinter window to show, just the File Explorer interface.
import tkinter
from tkinter import filedialog
import os
window = tkinter.Tk()
#window.geometry("1x1")
window.withdraw()
def open_files():
files = filedialog.askopenfiles(mode='r')
global filenames
filenames = [os.path.abspath(file.name) for file in files]
window.destroy() # analysis:ignore #says "window" is undefined becasue of "del window" below
window.after(0, open_files)
window.mainloop()
del window
The first time I run this in Spyder, if window.withdraw() is not commented out, the console just shows runfile(*my_file_name*) and the code does... something... in the background, but nothing seems to actually happen. Nothing changes on-screen, but I cannot type in the console so I know the code is running.
If I open a new console tab and run the code with window.withdraw() commented out, everything works, and the Tkinter GUI is visible. If I then run this code again in the same tab, with window.withdraw() not commented out, then the code works as intended, with only the File Explorer window opening up, and the Tkinter GUI staying hidden. This is the case even if I click the "Remove all variables" button in Spyder, so as far as I understand the code is not saving any variables that allow it to run properly after the first time.
My question is, why does this code work the 2nd, 3rd, 4th, etc. time I run it, but not the first time?
I kept playing around, and changed -alpha to alpha and got this error:
TclError: wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??"
So I ended up changing window.attributes('-alpha',0) to window.attributes('-topmost',True, '-alpha',0), and this works! It brings up File Explorer on the first run without showing the Tkinter window. Thank you #Thingamabobs for your help.
My final code is:
import tkinter
from tkinter import filedialog
import os
window = tkinter.Tk()
window.attributes('-topmost',True, '-alpha',0)
filenames = [os.path.abspath(file.name) for file in filedialog.askopenfiles(mode='r')]
window.destroy()
del window

How to cal tcl commands inside my python script using tkinter?

I am running my performance test on python and I want to call some Tcl commands using Tkinter inside my python script.
Can somebody please help me to write the code,
import Tkinter
root = Tkinter.Tk()
root.tk.eval('puts {printed by tcl}')
I tried simply above example, here when I do root=tkinter.tk() it opens up a window, I just want to execute my command and get the result
The code you have tried will not show any window until you put the root.mainloop(),but you can try something like this,
import tkinter
root = tkinter.Tk()
root.withdraw()
root.tk.eval('puts {printed by tcl}')
root.destroy()
root.mainloop()
here withdraw() will remove the window from the screen without destroying it and then you can perform your tasks and then destroy it at the end of code.

How to open a Tkinter app from a PyQt GUI

I have built an app with PySide2 (PyQt). Now I was asked to make a QPushButton, which simply starts an independent Tkinter app which someone else has programmed.
I thought I could simply connect the method below to my QPushButton which starts the Tkinter App (Modell_final.App is the app):
def open_tkinter():
root = tkinter.Tk()
app = Modell_final.App(root)
root.mainloop()
But when I push the Button inside my GUI the first time nothing happens and the second time the Tkinter App opens and freezes immediately.
Thanks for help :)
I am not sure how independent the Tkinter app is. If it is truly just some other small window you want to run that eventually gets closed again I would opt for an own process for it.
You can try it like this. Please save it as "test_test.py"
import tkinter as tk
import subprocess
window = tk.Tk()
window.title("Window Title")
window.geometry('200x100')
lbl = tk.Label(window, text="Hello World")
lbl.pack()
def clicked(): #function before bind
subprocess.Popen(['python' , 'test_test.py'])
btn = tk.Button(window, text="Click Me", command=clicked)
btn.pack()
window.mainloop()
If it is truly completly independet this would have the benefit that you do not even need to import the Modell_final into the PyQt app. Just import subprocess and let the button call the subprocesss.popen (notsubprocesss.call, then it freezes again) python file with the tkinter app in it.
The downside is
Sharing data with the Tkinter app is difficult, but if it is independent no issue here.
If the button is pressed multiple times multiple windows of the Tkinter app open up. This might be what you want, but if not you have to take care of this.

How do I end a Tkinter python module on mac so I don't have to force quit python launcher after the program is done?

This is my code:
from Tkinter import *
app = Tk()
app.title('example')
app.geometry('400x300+200+200')
b = Button(app, text = "quit", command = app.quit)
b.pack()
app.mainloop()
When I run this python launcher pops up and when I close the window or press the quit button the window closes and the python idle says its done but python launcher becomes unresponsive and I have to force quit it for python launcher to quit and disappear from the dock.
Is there a command that I have to use to exit the code properly?
You are looking for sys.exit()
Here is an example that works
from tkinter import *
import sys
class YourApp(Tk):
def quit_and_close(self):
app.quit()
sys.exit()
app = YourApp()
app.title('example')
app.geometry('400x300+200+200')
b = Button(app, text = "quit", command = app.quit_and_close)
b.pack()
app.mainloop()
I've been looking all over and it seems like this might have been a known bug.
The best response I've gotten is from:
window = tk.Tk()
window.protocol("WM_DELETE_WINDOW", window.destroy)
Which allows you to close the window with the little x. This ends the process in the spyder console window, but not the process in the applications bar on the mac. That must still be force quit.
I tried making a function like this to destroy the window and exit using sys, but this just causes the freeze again.
def CloseProgram():
sys.exit()
window.destroy()
Try using Terminal instead of an IDE. I've found that IDEs are very finicky, and using Terminal properly exits and quits the Python Launcher.

Categories

Resources