How to disconnect matplotlibs event handler? - python

I am trying to find a way to disconnect matplotlib's event handler by using mpl_disconnect. So far I followed the instructions here and here to learn how to disconnect but unfortunately it did not work for me.
With the following code I am able to connect button_press_event to the callback function on_press by using a checkbutton. After unchecking cid prints 0 so it should be disconnected but the callback function still fires.
I am using python 3.7.4 and matplotlib 3.1.1.
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.image as mpimg
class MainApplication(Tk.Frame):
def __init__(self, parent, *args, **kwargs):
Tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
parent.iconify
parent.grid_rowconfigure(1, weight=1)
parent.grid_columnconfigure(1, weight=1)
top_frame = Tk.Frame(parent)
top_frame.grid(row=0)
mid_frame = Tk.Frame(parent)
mid_frame.grid(row=1)
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
self.ax.set_aspect('equal')
canvas = FigureCanvasTkAgg(self.fig, mid_frame)
canvas.get_tk_widget().grid(row=0, column=0, sticky="nsew")
canvas._tkcanvas.grid(row=0, column=0, sticky="nsew")
img = mpimg.imread('stinkbug.png') # insert image file here
self.ax.imshow(img)
self.fig.canvas.draw()
self.var1 = Tk.IntVar()
chkbx1 = Tk.Checkbutton(top_frame, text='connect', variable=self.var1, command=self.de_activate)
chkbx1.grid(row=0, column=0, sticky="w")
def de_activate(self):
print('checkbutton: '+str(self.var1.get()))
self.cidpress = 0
if self.var1.get() == 1:
self.cidpress = self.fig.canvas.mpl_connect('button_press_event', self.on_press)
print('on_press connected (cid='+str(self.cidpress)+')')
else:
self.fig.canvas.mpl_disconnect(self.cidpress)
print('on_press disconnected (cid='+str(self.cidpress)+')')
def on_press(self, event):
if event.inaxes != self.ax: return
print('button pressed')
if __name__ == '__main__':
root = Tk.Tk()
MainApplication(root).grid(row=0, column=0, sticky="nsew")
root.mainloop()

To disconnect, you must pass the original cid to mpl_disconnect, but you're resetting self.cidpress before the if .. else .. block, so you're always requesting the disconnection of cid 0. Remove self.cidpress = 0 and place it right after self.fig.canvas.mpl_disconnect(self.cidpress):
def de_activate(self):
print('checkbutton: '+str(self.var1.get()))
if self.var1.get() == 1:
self.cidpress = self.fig.canvas.mpl_connect('button_press_event', self.on_press)
print('on_press connected (cid='+str(self.cidpress)+')')
else:
self.fig.canvas.mpl_disconnect(self.cidpress)
self.cidpress = 0 # <<<<<<<<<<<<<<<<<<<<
print('on_press disconnected (cid='+str(self.cidpress)+')')

Related

Python tkinter hangs when I try to close graphs

I've tried using Tk to make a function that will allow users to look at a graph and select if points are wrong. I had this working before, but now my code is hanging every time I run it and try to go to the next graph. Specifically when I click "next graph" and it should run
def _quit(self):
self.master.destroy() # stops mainloop
When I manually stop the code I get this message:
File "C:\Users\laura\Anaconda3\lib\tkinter\__init__.py", line 1429, in mainloop
self.tk.mainloop(n)
KeyboardInterrupt
Any suggestions? I've read using root.destroy() but I haven't been able to get that to work
Thanks!
def config_plot():
tk, ax = plt.subplots()
ax.set(title='Are the peaks okay?')
return (tk, ax)
class matplotlibSwitchGraphs:
def __init__(self, master):
self.master = master
self.frame = Frame(self.master)
self.frame.pack(expand=YES, fill=BOTH)
self.fig = Figure(figsize=(12,7))
self.ax = self.fig.gca()
self.canvas = FigureCanvasTkAgg(self.fig, self.master)
self.config_window()
self.graphIndex = 0
self.draw_graph_one()
def config_window(self):
toolbar = NavigationToolbar2Tk(self.canvas, self.master)
toolbar.update()
self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
print('connect')
self.canvas.mpl_connect("button_press_event", self.on_button_press)
self.button = Button(self.master, text="YES, next graph", command=self._quit) #this is where it gets stuck!
self.button.pack(side=BOTTOM)
self.button_switch = Button(self.master, text="NO", command=self.switch_graphs)
self.button_switch.pack(side=BOTTOM)
def draw_graph_one(self):
self.ax.clear() # clear current axes
self.ax.plot(data)
self.ax.plot(peaks,loc_peaks, marker='o', linestyle='none')
# self.ax.set(xlim=(touchdown_cut[-15]-50,toeoff_cut[-5]+50))
self.ax.set(title='Check the data')
self.canvas.draw()
def draw_graph_two(self):
self.ax.clear()
self.ax.plot(data)
self.ax.set(title='Click all the incorrect peaks')
self.ax.plot(peaks,loc_peaks, marker='o', linestyle='none')
self.canvas.draw()
def on_button_press(self, event):
print('xdata, ydata:', event.xdata, event.ydata)
# return (event.xdata)
global ix
ix = event.xdata
global clicks
clicks.append((ix))
array1 = np.asarray(peaks)
idx1 = (np.abs(array1 - event.xdata)).argmin()
global peaks_adj
global loc_peaks_adj
peaks_adj = np.delete(peaks, idx1)
loc_peaks_adj = np.delete(loc_peaks, idx1)
self.canvas.flush_events()
self.ax.clear()
self.ax.plot(data)
self.ax.set(title='Click all the incorrect peaks')
self.ax.plot(peaks_adj,loc_peaks_adj, marker='o', linestyle='none')
self.canvas.draw()
return (clicks)
def _quit(self):
self.master.destroy() # stops mainloop
def switch_graphs(self):
# Need to call the correct draw, whether we're on graph one or two
self.graphIndex = (self.graphIndex + 1 ) % 2
if self.graphIndex == 0:
self.draw_graph_one()
else:
self.draw_graph_two()
def main():
root = Tk()
matplotlibSwitchGraphs(root)
root.mainloop()
if __name__ == '__main__':
main()
Try changing root.destroy() to self.destroy()as you are working with classes. If using function through a button remove the()after command likecommand=self.destroy`.
Also don't use global, use self. to create a class variable if you want to access the variable in multiple function.
As far as for lags, I would recommend you to use update and update_idletasks.

How to insert scrollbar into canvas filled with buttons?

I'm trying to insert a scrollbar into a canvas filled with buttons (where, buttons can be switched to clickable text as well).
With the code below, the window seems OK.
However, when I uncomment the lines with the scrollbar, the window geometry scrambles.
Can anybody give a hand to figure out what's wrong with the code?
System: Windows 10, python 3.9.6.
import tkinter as tk
class tWindow(tk.Tk):
frame = None
canvas = None
scrollbar = None
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.geometry("640x480+50+50")
self.setup_frame()
self.setup_canvas()
def setup_frame(self):
self.frame = tk.Frame(master=self, width=640, height=480)
self.frame.configure(bg="#003163")
self.frame.place(x=0, y=0)
def setup_canvas(self):
self.update()
self.canvas = tk.Canvas(master=self.frame, bg="#006331",
width=int(self.frame.winfo_width() / 4), height=self.frame.winfo_height())
self.canvas.place(x=0, y=0)
self.update()
# self.scrollbar = tk.Scrollbar(master=self.canvas, orient=tk.VERTICAL)
# self.scrollbar.pack(side=tk.RIGHT, fill=tk.BOTH, expand=tk.TRUE)
# self.canvas.configure(yscrollcommand=self.scrollbar.set)
# self.scrollbar.configure(command=self.canvas.yview)
# self.update()
tmp_pos = 0
for i in range(20):
btn_tmp = tk.Button(master=self.canvas, text=f"testing testing testing testing testing {i:02} ...",
justify=tk.LEFT, wraplength=self.canvas.winfo_width(), bg="#00c0c0", fg="#000000")
btn_tmp.place(x=0, y=tmp_pos)
self.update()
tmp_pos = btn_tmp.winfo_y() + btn_tmp.winfo_height()
def main():
app = tWindow()
app.mainloop()
if __name__ == "__main__":
main()
See comments in code as additional to the link I have provided. Also see why update is considered harmfull and how do I organize my tkinter gui for more details.
import tkinter as tk
class tWindow(tk.Tk):
frame = None
canvas = None
scrollbar = None
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.geometry("640x480+50+50")
self.setup_frame()
self.setup_canvas()
def setup_frame(self):
self.frame = tk.Frame(master=self, width=640, height=480,bg="#003163")
self.frame.pack(side='left',fill='both',expand=True)##pack left
## self.frame.place(x=0, y=0) ##place requiers too think to much
def setup_canvas(self):
## self.update() ##update is harmfull
self.canvas = tk.Canvas(master=self.frame, bg='orange',#"#006331",
width=int(self.frame.winfo_reqwidth() / 4), height=self.frame.winfo_reqheight())
##use reqwidth/hight to avoid the use of update.
##It takes the requested size instead the actual
self.canvas.pack(side='left')
self.canvas_frame = tk.Frame(self.canvas,bg="#006331")
##frame to pack buttons in canvas
self.canvas.create_window((0,0), window=self.canvas_frame, anchor="nw")
##show frame in canvas
self.scrollbar = tk.Scrollbar(master=self.frame, orient=tk.VERTICAL)
self.scrollbar.pack(side='left',fill='y')
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.scrollbar.configure(command=self.canvas.yview)
self.canvas_frame.bind('<Configure>',self.oncanvasconfigure)
##bind event configure to update the scrollregion
for i in range(20):
btn_tmp = tk.Button(master=self.canvas_frame, text=f"testing testing testing testing testing {i:02} ...",
justify=tk.LEFT, wraplength=self.canvas.winfo_reqwidth(), bg="#00c0c0", fg="#000000")
btn_tmp.pack()
def oncanvasconfigure(self,event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
def main():
app = tWindow()
app.mainloop()
if __name__ == "__main__":
main()

communication between tkinter toplevels

I'm working on a little project and made a little on-screen keyboard as a tkinter Toplevel
my application is buildt like this:
Root-Window (Tk-Widget)
input 1 (Entry-Widget)
input 2 (Entry-Widget)
input 3 (Text-Widget)
on_screen-keyboard (Toplevel-Widget)
the Toplevel-Widget contains Buttons, with callbacks that should enter text in the entries (just like keyboard-Buttons)
What I want is a communication between children of the keyboard/the keyboard and the last active input-Widget. My Problem is, that I don't know, how to say the keyboard, to which input-Widget it should send the message.
import tkinter as tk
from tkinter import ttk
class MainWindow(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.active_input = tk.Variable(value=None)
ttk.Button(self, text="show Keyboard", command=lambda: Keyboard(self)).pack()
self.text = tk.StringVar(value="")
self.input1 = ttk.Entry(self)
self.input1.bind("<FocusIn>", lambda e: self.active_input.set(self.input1))
self.input2 = ttk.Entry(self)
self.input2.bind("<FocusIn>", lambda e: self.active_input.set(self.input2))
self.input3 = tk.Text(self, height=3, width=15)
self.input3.bind("<FocusIn>", lambda e: self.active_input.set(self.input3))
self.input1.pack()
self.input3.pack()
self.input2.pack()
class Keyboard(tk.Toplevel):
OPENED = False
NAME = "- Keyboard -"
NUM = [{"text":"1", "width":1},
{"text":"2", "width":1},
{"text":"3", "width":2}]
CHAR= [{"text":"A", "width":1},
{"text":"B", "width":1},
{"text":"C", "width":2}]
def __init__(self, master):
if not Keyboard.OPENED:
Keyboard.OPENED = True
print("keyboard opened!")
self.master = master
tk.Toplevel.__init__(self, master)
self.title(self.NAME)
self.protocol("WM_DELETE_WINDOW", self.close)
self.keyb_nb = ttk.Notebook(self)
self.keyb_nb.pack()
self.num_tab = ttk.Frame(self.keyb_nb)
self.createPad(self.num_tab, Keyboard.NUM,2)
self.keyb_nb.add(self.num_tab, text="123")
self.char_tab = ttk.Frame(self.keyb_nb)
self.createPad(self.char_tab, Keyboard.CHAR, 2)
self.keyb_nb.add(self.char_tab, text="ABC")
def createPad(self, master, pad:list, max_col):
self.co_count = 0
self.ro = 1
for button in pad:
button["id"] = ttk.Button(master, width=6*button["width"], text=button["text"], command=self.bclicked(button))
if self.co_count >= max_col:
self.ro = self.ro + 1
self.co_count = 0
button["id"].grid(row=self.ro, columnspan=button["width"], column=self.co_count)
self.co_count = self.co_count+button["width"]
def bclicked(self, button:dict):
"""
reciver = self.master.active_input #I think the Problem here is, that the variable contains a string, not a widget
reciever.focus_force()
reciever.insert(index=tk.INSERT, string=button["text"])
"""
pass
def close(self):
Keyboard.OPENED = False
self.destroy()
print("keyboard closed!")
root = MainWindow()
root.mainloop()
Here the init of the Mainwindow and the bclicked of the Keyboard class are important...
the code is debug-ready
I would prefer a solution, similar to the communication in the internet (sender=button, receiver-id, message), but very welcome every working solution
btw: I'm also looking for a solution, how I don't have to force the input to focus and the Toplevel stays an the highest layer of the screen (that if I focus the Tk-Widget/one of the inputs/the button, the keyboard will stay in front of it)
SUMMARY: how do I find out, which of the 3 input-widgets was active at last, when the keyboard-toplevel has already the focus?
I may made more changes than needed, but mainly focus on the method keyboard_triger() and pass_key_to_master(), this two use the idea that the variable master implements, having access to call methods out of scope.
Olso the method set_focused_object() stores a reference to the last object beeng focused, note than it stores the widget and not the event, it's easyer than searching each time the object
import tkinter as tk
from tkinter import ttk
class MainWindow(tk.Tk):
def keyboard_triger(self, key):
# to identify wath object is just use
# isinstance(self.active_input, ttk.Entry)
self.active_input.insert(tk.END, key)
def new_keyboard(self):
Keyboard(self)
def set_focused_object(self, event):
self.active_input = event.widget
def __init__(self):
tk.Tk.__init__(self)
self.active_input = None
ttk.Button(self, text="Show Keyboard", command=self.new_keyboard).pack()
self.input1 = ttk.Entry(self)
self.input1.bind("<FocusIn>", self.set_focused_object)
self.input1.pack()
self.input2 = ttk.Entry(self)
self.input2.bind("<FocusIn>", self.set_focused_object)
self.input2.pack()
self.input3 = tk.Text(self, height=3, width=15)
self.input3.bind("<FocusIn>", self.set_focused_object)
self.input3.pack()
class Keyboard(tk.Toplevel):
def pass_key_to_master(self, key):
self.master.keyboard_triger(key)
def __init__(self, master):
tk.Toplevel.__init__(self, master)
self.master = master
self.title('Keyboard')
# this way of agruping keys stores the kwags
# of the drawing method
keys = {
'A': {'x': 0, 'y': 0},
'B': {'x': 20, 'y': 20},
'C': {'x': 50, 'y': 50}
}
# expected structure
# {string key: reference to the button}
self.buttons = {}
for i in keys:
self.buttons[i] = tk.Button( # i=i is required to make a instance
self, text=i, command=lambda i=i: self.pass_key_to_master(i)
)
self.buttons[i].place(**keys[i])
if __name__ == '__main__':
root = MainWindow()
root.mainloop()
Your code maybe could have a better construction.(But I didn't revise your code construction.)
Followed by your code,I use a global variable.And fix some errors in your code.And it could work normally in my computer.
import tkinter as tk
from tkinter import ttk
class MainWindow(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.active_input = tk.Variable(value=None)
ttk.Button(self, text="show Keyboard", command=lambda: Keyboard(self)).pack()
global focusedWidget
focusedWidget = None
self.text = tk.StringVar(value="")
self.input1 = ttk.Entry(self)
self.input1.bind("<FocusIn>", self.getFocusWidget)
self.input2 = ttk.Entry(self)
self.input2.bind("<FocusIn>", self.getFocusWidget)
self.input3 = tk.Text(self, height=3, width=15)
self.input3.bind("<FocusIn>", self.getFocusWidget)
self.input1.pack()
self.input3.pack()
self.input2.pack()
def getFocusWidget(self,event): # this function could be a static function
global focusedWidget
focusedWidget = event.widget
class Keyboard(tk.Toplevel):
OPENED = False
NAME = "- Keyboard -"
NUM = [{"text":"1", "width":1},
{"text":"2", "width":1},
{"text":"3", "width":2}]
CHAR= [{"text":"A", "width":1},
{"text":"B", "width":1},
{"text":"C", "width":2}]
def __init__(self, master):
if not Keyboard.OPENED:
Keyboard.OPENED = True
print("keyboard opened!")
self.master = master
tk.Toplevel.__init__(self, master)
self.title(self.NAME)
self.protocol("WM_DELETE_WINDOW", self.close)
self.keyb_nb = ttk.Notebook(self)
self.keyb_nb.pack()
self.num_tab = ttk.Frame(self.keyb_nb)
self.createPad(self.num_tab, Keyboard.NUM,2)
self.keyb_nb.add(self.num_tab, text="123")
self.char_tab = ttk.Frame(self.keyb_nb)
self.createPad(self.char_tab, Keyboard.CHAR, 2)
self.keyb_nb.add(self.char_tab, text="ABC")
def createPad(self, master, pad:list, max_col):
self.co_count = 0
self.ro = 1
for button in pad:
button["id"] = ttk.Button(master, width=6*button["width"], text=button["text"], command=lambda button=button:self.bclicked(button)) # this lambda expression has some errors.
if self.co_count >= max_col:
self.ro = self.ro + 1
self.co_count = 0
button["id"].grid(row=self.ro, columnspan=button["width"], column=self.co_count)
self.co_count = self.co_count+button["width"]
def bclicked(self, button:dict):
global focusedWidget
"""
reciver = self.master.active_input #I think the Problem here is, that the variable contains a string, not a widget
reciever.focus_force()
reciever.insert(index=tk.INSERT, string=button["text"])
"""
if not focusedWidget: # If user hasn't click a entry or text widget.
print("Please select a entry or text")
return
if focusedWidget.widgetName=='ttk::entry': # use if statement to check the type of selected entry.
focusedWidget.insert(index=tk.INSERT,string=button["text"])
else:
focusedWidget.insert("end",button["text"])
def close(self):
Keyboard.OPENED = False
self.destroy()
print("keyboard closed!")
root = MainWindow()
root.mainloop()

Tkinter, error maximum recursion depth exceeded

I am having trouble writting a tkinter application with matplotlib that updates dinamicaly. I create a plot and use it inside the tkinter window. Then the plot is updated every 50ms with a tk.after() method, which works fine in other applications I have tryed so far. But in my application everything seems to be working until after some time (a minute or so) I get an error:
RecursionError: maximum recursion depth exceeded while calling a Python object
The code is:
import matplotlib
#matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import sys
import time
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
import numpy as np
import matplotlib.pyplot as plt
import math
class MainWindow():
#----------------
def __init__(self, root):
self.index=0
self.root=root
self.fig, self.ax = plt.subplots()
self.line, = self.ax.plot(np.random.randn(100))
#plt.show(block=False)
# a tk.DrawingArea
self.canvas = FigureCanvasTkAgg(self.fig, master=root)
self.canvas.show()
self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
self.toolbar = NavigationToolbar2TkAgg(self.canvas, root)
self.toolbar.update()
self.canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
self.canvas.mpl_connect('key_press_event', self.on_key_event)
self.display = Tk.Label(root, text="") # we need this Label as a variable!
self.display.pack()
self.button1 = Tk.Button(master=root, text='Quit', command=self._quit)
self.button1.pack(side=Tk.BOTTOM)
global w
w=2*math.pi
self.button2 = Tk.Button(master=root, text='Increase frecuency', command=self.button2_event)
self.button2.pack(side=Tk.BOTTOM)
#A simple clock
global miliseconds
global t
t=time.time()
self.update_clock()
print('going to the next stop')
self.root.mainloop()
def on_key_event(event):
print('you pressed %s' % event.key)
key_press_handler(event, self.canvas, toolbar)
def _quit(self):
self.root.quit() # stops mainloop
self.root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
def button2_event(self):
global t
global w
w+=2*3.1416
def update_clock(self):
global t
mili=str(math.floor(1000*(t-time.time())))+' ms'
t=time.time()
now = time.strftime("%H:%M:%S"+'-'+mili)
self.display.configure(text=now)
N=100
y=np.sin(4*math.pi*(np.array(range(N))/N)+t*w)
x=range(N)
self.line.set_ydata(y)
self.line.set_xdata(x)
self.ax.relim()
self.ax.autoscale()
#☺fig.canvas.update()
#fig.canvas.flush_events()
self.canvas.show()
self.canvas.flush_events()
self.root.after(50, self.update_clock()) #<------ERROR HERE!------
root = Tk.Tk()
root.wm_title("Embedding in TK")
MainWindow(root)
Change self.root.after(50, self.update_clock()) to self.root.after(50, self.update_clock), after
after(delay_ms, callback=None, *args)
Registers an alarm callback that is called after a given time.

Python: Have gifs animate with TKinter while a thread is running

So, as I'm sure you can tell from my rather long subject what I'm asking but to reiterate. I am trying to have a gif animate while a thread in the background is running a calculation with in TKinter. For simplicities sake, I am using this gif: http://i.imgur.com/4Y9UJ.gif
The gif animation is currently working as you can see if you run the script. But as soon as the Generate button is pressed and the thread initiates, the gif pauses until it finishes. I'm using time.sleep() to simulate the large amount of calculations that I will be doing in the background (though I am unsure if this is what is actually causing the issue).
I'm sure it has something to do with not understanding exactly how the animation is functioning. Any advice?
Code as follows:
from Tkinter import *
import tkMessageBox
import time
import os
import threading
from PIL import Image, ImageTk
class Gif(Label):
def __init__(self, master, filename):
evanGif = Image.open(filename)
gifSeq = []
try:
while 1:
gifSeq.append(evanGif.copy())
evanGif.seek(len(gifSeq)) # skip to next frame
except EOFError:
pass # we're done
try:
#Special case for the evangalion gif
if evanGif.info['duration'] == 0:
self.delay = 100
else:
self.delay = evanGif.info['duration']
except KeyError:
self.delay = 100
gifFirst =gifSeq[0].convert('RGBA')
self.gifFrames = [ImageTk.PhotoImage(gifFirst)]
Label.__init__(self, master, image=self.gifFrames[0])
temp =gifSeq[0]
for image in gifSeq[1:]:
temp.paste(image)
frame = temp.convert('RGBA')
self.gifFrames.append(ImageTk.PhotoImage(frame))
self.gifIdx = 0
self.cancel = self.after(self.delay, self.play)
def play(self):
self.config(image=self.gifFrames[self.gifIdx])
self.gifIdx += 1
if self.gifIdx == len(self.gifFrames):
self.gifIdx = 0
self.cancel = self.after(self.delay, self.play)
class App:
generating = False
def __init__(self, master):
self.master=master
#Initializing frames
self.buttonFrame = Frame(master, background='light gray')
self.loadingFrame = Frame(master, background='light gray')
self.loadingFrame.grid(row=0)
self.buttonFrame.grid(row=1)
self.anim = Gif(self.loadingFrame, '4Y9UJ.gif').pack()
self.update_Thread = threading.Thread(target=time.sleep, args=(5,))
self.buttonSetup()
def buttonSetup(self):
#ALL THE BUTTONS
self.generateBt = Button(self.buttonFrame, text="Generate!", command=self.generate, background='light gray', highlightbackground='light gray')
self.generateBt.pack(side=LEFT)
self.quitBt = Button(self.buttonFrame, text="Quit!", fg="red", command=self.buttonFrame.quit, background='light gray', highlightbackground='light gray')
self.quitBt.pack(side=LEFT)
def generate(self):
self.hideForGen()
self.update_Thread.start()
while(self.update_Thread.isAlive()):
self.master.update_idletasks()
self.reset()
self.master.update_idletasks()
tkMessageBox.showinfo("Complete", "Report generation completed!")
def hideForGen(self):
self.buttonFrame.grid_forget()
def reset(self):
self.buttonFrame.grid(row=1)
root = Tk()
root.title('Test')
root.configure(background='light gray')
app = App(root)
root.mainloop()
The trouble is in your generate() method.
That while loop is unnecessary.
Bear in mind that you can't re-start a thread.
If you want to use the generate button multiple times, you'll need to create a new thread each time.
class App:
generating = False
def __init__(self, master):
self.master=master
#Initializing frames
self.buttonFrame = Frame(master, background='light gray')
self.loadingFrame = Frame(master, background='light gray')
self.loadingFrame.grid(row=0)
self.buttonFrame.grid(row=1)
self.anim = Gif(self.loadingFrame, '4Y9UJ.gif')
self.anim.pack()
## self.update_Thread = threading.Thread(target=time.sleep, args=(5,))
self.buttonSetup()
def buttonSetup(self):
#ALL THE BUTTONS
self.generateBt = Button(self.buttonFrame, text="Generate!", command=self.generate, background='light gray', highlightbackground='light gray')
self.generateBt.pack(side=LEFT)
self.quitBt = Button(self.buttonFrame, text="Quit!", fg="red", command=self.buttonFrame.quit, background='light gray', highlightbackground='light gray')
self.quitBt.pack(side=LEFT)
def wait_generate(self):
if self.update_Thread.isAlive():
self.master.after(500, self.wait_generate)
else:
tkMessageBox.showinfo("Complete", "Report generation completed!")
self.reset()
def generate(self):
self.hideForGen()
self.update_Thread = threading.Thread(target=time.sleep, args=(5,))
self.update_Thread.start()
## while(self.update_Thread.isAlive()):
## self.master.update_idletasks()
## self.reset()
## self.master.update_idletasks()
## tkMessageBox.showinfo("Complete", "Report generation completed!")
self.wait_generate()
def hideForGen(self):
self.buttonFrame.grid_forget()
def reset(self):
self.buttonFrame.grid(row=1)
In the method below:
def generate(self):
self.hideForGen()
self.update_Thread.start()
while(self.update_Thread.isAlive()):
self.master.update_idletasks()
self.reset()
self.master.update_idletasks()
tkMessageBox.showinfo("Complete", "Report generation completed!")
If you put
self.master.update()
instead of
self.master.update_idletasks()
it should work. At least it did for me.

Categories

Resources