Tkinter 'quit' button must be pressed repeatedly for multiple windows - python

I'm working on a Tkinter GUI that displays matplotlib figures. I use the variable 'window' to initialize the Tk interpreter; instead of 'root' / 'main' = Tk(). The GUI is formatted by the class MyWindow.
class MyWindow:
def __init__(self, win):
Three GUI 'visual' buttons display matplotlib figures. The functions for the mpl figures are in a module called 'charts'. In the below example, 'overview' is a visual button.
self.btn5=Button(win, text='Overview')
self.b5=Button(win, text='Overview', width='6', height='2', command=self.overview)
self.b5.place(x=50, y=160)
def overview(self):
from charts import overview
overview()
A 'quit' button exits the visual windows and the GUI itself. Quit button code:
self.btn6=Button(win, text='Quit')
self.b6=Button(win, text='Quit', width='6', height='2', command=window.quit)
self.b6.place(x=50, y=230)
def quit(self):
self.win.destroy()
All features work as intended, barring the 'quit' button. I must click 'quit' as many times as the number of windows the GUI opened for it to work.
i.e. I launch the GUI and open the 'overview' visual; I need to click 'quit' twice to close 'overview' and the GUI itself.
I've tried adjusting both the quit button 'command' and the quit function to all combinations of destroy() / quit() with prefixes self, win, and window (and no prefix).

Try to use Python inbuilt function called quit() it will just quit the program killing its processes.
def quit(self):
quit()

Related

Is there a simple way to wait for an event to happen?

Alright, so all I want is to wait for a function to be run and then continue the rest of the code. Basically, all I want to do is wait until the user presses a Tkinter button, and then after that start the rest of the code.
from tkinter import *
def function():
[insert something here]
root=Tk()
btn1=Button(root, command=function)
Now wait until the user presses the button and then continue
print("Yay you pressed a button!")
If you want to continue some code after pressing button then put this code inside function()
import tkinter as tk # PEP8: `import *` is not preferred
# --- functions ---
def function():
print("Yay you pressed a button!")
# ... continued code ...
# --- main ---
root = tk.Tk()
btn1 = tk.Button(root, command=function)
btn1.pack() # put button in window
root.mainloop() # run event loop which will execute function when you press button
PEP 8 -- Style Guide for Python Code
EDIT:
GUIs don't work like input() which waits for user data. Button doesn't wait for user's click but it only inform mainloop what it has to display in window. And mainloop runs loop which gets key/mouse events from system and it checks which Button was clicked and it runs assigned function. GUI can't stops code and wait for button because it would stop also other widgets in window and it would look like it frozen.
The same is in other GUIs - ie. PyQt, wxPython, Kivy - and other languages - ie. Java, JavaScript.

tkinter wiget.bind('<Activate>', callback) not working

I've been trying to create a message box when a button turns from disabled to active but when the button actually turns from disabled to active for some reason my callback is not even being called I've tried to get it working so for quite a bit of time now and I'm stuck.
Here is an example of the problem:
from tkinter import *
from tkinter import Tk
def disable_and_activate():
b.config(state = DISABLED)
b.config(state = ACTIVE)
def is_working(event):
print('working')
root = Tk()
b = Button (root, text = 'click me', command = disable_and_activate)
b.pack()
b.bind('<Activate>', is_working)
root.mainloop()
Console:
the button is clicked but there's nothing printed on the console
The <Activate> event is not triggered when you set the state of the button to "active". The event is triggered when the window becomes the active window.
For example, when I run your code on my OSX machine, if I click on some other application to give it focus and then I click back to the tkinter window, the event will fire when the tkinter window becomes the active window.
This is explained in the canonical tcl/tk documentation which says this:
Activate, Deactivate
These two events are sent to every sub-window of a toplevel when they change state. In addition to the focus Window, the Macintosh platform and Windows platforms have a notion of an active window (which often has but is not required to have the focus). On the Macintosh, widgets in the active window have a different appearance than widgets in deactive windows. The Activate event is sent to all the sub-windows in a toplevel when it changes from being deactive to active. Likewise, the Deactive event is sent when the window's state changes from active to deactive. There are no useful percent substitutions you would make when binding to these events.
Here the problem was just the code inside the function , It seems like you needed to call EventGenerate('<<Activate>>') I also recommend adding 2 << and 2 >>
So I rewrote the code and its now working perfectly fine:
from tkinter import *
from tkinter import Tk
import tkinter
def disable_and_activate():
b.configure(state=tkinter.DISABLED)
b.configure(state=tkinter.ACTIVE)
b.event_generate("<<Activate>>")
def is_working(event):
print('working')
root = Tk()
b = Button (root, text = 'click me', command = disable_and_activate)
b.pack()
b.bind('<<Activate>>', is_working)
root.mainloop()

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?

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

Run function from Python 2 Tkinter button when pressed then run another when released

Hello I am trying to make a simple recorder in Python 2.7 using Tkinter as the GUI, I want to be able to record when the button is pressed then save the recording when the button is released, I know how to make the button and have already done so, but I don't know how to make it run a program when pressed and another when released, is it possible?
Also I'm not sure how to actually record from the microphone and save it using pyaudio, any help with this is appreciated but I'm sure I can figure this out myself when I have overcome the main issue.
You can bind an event to the click of the left mouse button <Button-1> and to the release of the left mouse button <ButtonRelease-1>. Here's an example:
import Tkinter as tk
root = tk.Tk()
def clicked(event):
var.set('Clicked the button')
def released(event):
var.set('Released the button')
var = tk.StringVar()
var.set('Nothing to see here')
label = tk.Label(root, textvar=var)
label.pack()
but = tk.Button(root, text='Button')
but.bind("<Button-1>", clicked)
but.bind("<ButtonRelease-1>", released)
but.pack()
root.mainloop()

Categories

Resources