How to unite 2 scripts in one using tkinter? - python

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

Related

Correct way to run two Tk() mainloops independently, with second being started from first script?

script_a.py
from tkinter import *
from script_b import test
root = Tk()
var1 = stuff
var2 = stuff
def start_scriptb():
test([var1, var2])
Button(root, text="Start",
command=start_scriptb)
root.mainloop()
script_b.py
from tkinter import *
def test(x):
main = Tk()
Label(main, text=x)
Button(main, text="Exit", command=main.destroy())
main.mainloop()
This is a very basic version of what I'm trying to achieve. I actually am spawning a progress window that uses subprocess.Popen in script 2 with the passed through variables from script one and viewing the progess through a scrolled text widget on the 2nd program. I'm trying to spawn a new process, independent from the root GUI each time that button is hit from script_a.
It works fine in my tests so far, but I wanted to see if this could cause any issues or if this is actually spawning two processes?
I know there should only be one Tk() per process.
Using a TopLevel() window to show the progress works fine with the threading module, but the TopLevel() window will freeze as well as root (and any other open TopLevels()) if root is doing any sort of processing that takes any length of time.
With many hours of testing, I did have success in running two Tk() loops, but it had potential to be problematic, as "Bryan Oakley" had posted in many threads about.
Ultimately, I decided when I was in need of running something alone, I'd start my GUI with arguments and process it in an entirely new process instead of passing any arguments directly. Seems like a safer option.

Is there a way to make a custom askopenfilenames dialog window or at least edit its title bar, close button, etc in tkinter?

I'm making an app in tkinter which uses the ttk.Scale widget to show the process of an mp3 song.
I have a function that I want to add buttons with the names of which (the buttons) should be relied on filenames. Therefore I've made this example:
from tkinter import Tk, Button
from tkinter.filedialog import askopenfilenames
from tkinter.ttk import Scale
from threading import Timer
root = Tk()
slider = Scale(root, from_=0, to=100, orient='horizontal')
slider.pack()
# slider is continuously set to a bigger number so that it keeps going
def update_slider(num):
slider.set(num)
num += 1
root.after(50, update_slider, num)
update_slider(num=0)
# this function creates buttons based on the files opened
def add_buttons():
# the 'X' button of this particular window slows down execution of update_slider function
files = askopenfilenames(title='Add Buttons')
for i in list(files):
Button(root, text=i).pack()
button = Button(root, text='Browse', command=lambda: Timer(0.1, add_buttons).start())
button.pack()
root.mainloop()
The problem I'm facing is that when I open the askopenfilenames dialog box or when I press its 'X' button, my slider which is running continuously in the background gets stuck, and as a result doesn't show the process correctly.
Here is a picture where I hold down the 'X' button and the ttk.Scale stops moving:
I've tried using threading to run the add_buttons function but the behavior of the program remains the same.
Can I edit the askopenfilenames dialog box with something similar like overrideredirect(True) so that I can make my own title bar and 'X' button and the events generated not to slow down my Scale?
Replying to:
I cannot reproduce the issue in Linux, the scale keeps moving no matter what I do with the filedialog window. So this may be an OS specific issue.
I'm aware that this problem doesn't appear on Linux. I faced the same problem with the root's close button and other Toplevels' close button, but I fixed it by replacing the title bar using overrideredirect(True).
Is there anything similar I can do with this askopenfilenames 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.

Closing a window in python 3.7.2 with tkinter

I've made a program in python with Tkinter that allows you to free draw and choose different colors. I decided to make a button that would close the window instead of clicking the exit button in the top right corner. My question is how do I make the window close when the button is pressed?
If you are using a main loop for your application, then you can use the .destroy() method to release all the resources associated with the window and close the application. You call this method within the command function for your button like so:
from tkinter import *
root = Tk()
frame = Frame(root)
frame.pack(side=LEFT)
button = Button(frame, text="Exit", command=exit)
button.pack()
root.mainloop()
def exit():
root.destroy()
That should close your window. Optionally, the destroy() method may also be used at the end of your main loop if the X button of your application won't close the window immediately.
See these examples for more info:
http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.destroy-method
http://effbot.org/tkinterbook/tkinter-hello-again.htm

Categories

Resources