Tkinter - dragging mouse across disabled Textbox stops update() - python

I'm making a program that uses a some kind of widget that from the user's side, lists 'uneditable' and 'unselectable' lines of data.
The widget should also have a scrollbar so my widget options are a bit more limited from what I understand.
Furthermore, the application displays continuously updating numbers.
I went for a textbox - however, when I hold down left click and move my mouse across the textbox root.update() stops/waits.
I wrote some example code below to demonstrate this phenomenon.
import time
from tkinter import *
class App:
def __init__(self):
self.root = Tk()
self.root.geometry("500x500")
self.root.resizable(False, False)
self.main_frame = Frame(self.root)
self.main_frame.grid(row = 0, column = 0, sticky = "news")
self.main_text_box = Text(self.main_frame)
self.main_text_box.grid(row = 0, column = 0, sticky = "news")
self.main_text_box.tag_configure("bold", font = "Helvetica 50")
self.main_text_box.insert(END, "Example text", "bold")
self.main_text_box.configure(state = DISABLED)
def update(self):
self.root.update()
def main():
application = App()
time_start = time.time()
while True:
application.update()
print("Program running, {} seconds since start".format(
round(time.time() - time_start, 3)))
if __name__ == "__main__":
main()
When the user drags the mouse across the textbox, the print statement in
while True:
waits for root.update().
Basically, my question is: Is there any way to not have root.update() wait if the mouse is dragged across a disabled textbox?
(note - I'm new to this site so if I'm being unclear or something, please do point out what I could've done better in my question :))
Thanks!
edit: Sorry, I forgot to mention that I'm using update() because in my actual program (which I didn't post b/c it's 800+ lines), I have other non-tkinter update() methods in the while loop so that I can update other data each frame.

You do not need to manage the loop of the tkinter instance with update() the mainloop() will do this for you.
Instead lets write this where the class inherits from Tk() and then make the time print function part of the class. We can also use after() to update the print. Lastly we should set time_start to a class attribute to be used in our time function.
import time
import tkinter as tk
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.geometry("500x500")
self.resizable(False, False)
self.time_start = time.time()
self.main_frame = tk.Frame(self)
self.main_frame.grid(row = 0, column = 0, sticky = "news")
self.main_text_box = tk.Text(self.main_frame)
self.main_text_box.grid(row = 0, column = 0, sticky = "news")
self.main_text_box.tag_configure("bold", font = "Helvetica 50")
self.main_text_box.insert("end", "Example text", "bold")
self.main_text_box.configure(state = "disabled")
self.time_check()
def time_check(self):
print("Program running, {} seconds since start".format(round(time.time() - self.time_start, 3)))
self.after(100, self.time_check)
if __name__ == "__main__":
my_app = App()
my_app.mainloop()

I could not quite reproduce the problem described; however, some anti-patterns need correction in the code posted:
Use mainloop instead of a while loop.
avoid calling update, the mainloop handles that for you.
Use root.after to repeatedly call a method.
I change your App class to have it inherit from tk.Tk; you could inherit from Frame instead; in this case, you have to provide a master to it.
I also placed the console printing in a function outside the class, as it felt more suitable to keep a console output separated.
import time
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry("500x500")
self.resizable(False, False)
self.main_frame = Frame(self)
self.main_frame.grid(row = 0, column = 0, sticky = "news")
self.main_text_box = Text(self.main_frame)
self.main_text_box.grid(row = 0, column = 0, sticky = "news")
self.main_text_box.tag_configure("bold", font = "Helvetica 50")
self.main_text_box.insert(END, "Example text", "bold")
self.main_text_box.configure(state = DISABLED)
self._update_me()
def _update_me(self):
print_time()
self.after(500, self._update_me)
def print_time():
print("Program running, {} seconds since start".format(
round(time.time() - time_start, 3)))
def main():
application = App()
application.mainloop()
if __name__ == "__main__":
time_start = time.time()
main()

Related

Ttk Indeterminate progress bar on button press

I am trying to create a progress bar that runs as long as my function is running to show the user that things are happening and not just frozen. My function (generate_reports) makes queries to the database and writes to CSV files. Here is an abstract version of my code:
from tkinter import *
from tkinter import ttk
from billing import generate_reports
class app:
def __init__(self, root):
self.mainframe = ttk.Frame(root, padding = '4 4 12 12')
self.mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
ttk.Button(self.mainframe, text = "Generate Billing Reports", command = self.do_reports).grid(column = 2, row = 3, sticky = (W, E))
def do_reports(self, *args):
pbar = ttk.Progressbar(self.mainframe, orient = HORIZONTAL, mode = 'indeterminate')
pbar.grid(row = 4, column = 3, sticky = (W, E))
t1 = threading.Thread(target = generate_reports, args = [start, end])
t1.start()
pbar.start()
t1.join()
pbar.stop()
return
root = Tk()
BillingApp(root)
root.mainloop()
With this code, the progress bar doesn't pop up until after the generate_reports thread is completed and it is unmoving. If I remove the join, everything works fine but it never stops loading. How can I make the loading bar run for only the duration of the generate_reports thread?
Heh welcome to the fun world of event driven programming :). You can't use join here, the point of that function is to block until the thread is done and the whole point of using a thread is to avoid blocking the mainloop. You have 2 choices: either set up the GUI to constantly poll the thread to see if it's still running, or set up the thread to send a message back to the GUI when it's done. This latter option is probably the cleanest, and it's often done using tkinter's event mechanism.
from tkinter import *
from tkinter import ttk
import threading
import time
def generate_reports(start, end):
print("provide a mcve next time!")
time.sleep(5)
def run_report(root, *args):
generate_reports(*args)
root.event_generate("<<PhishDoneEvent>>") # yes, this is using tkinter in a thread, but some tkinter methods are ok to use in threads
class BillingApp:
def __init__(self, root):
self.mainframe = ttk.Frame(root, padding = '4 4 12 12')
self.mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
ttk.Button(self.mainframe, text = "Generate Billing Reports", command = self.do_reports).grid(column = 2, row = 3, sticky = (W, E))
root.bind("<<PhishDoneEvent>>", self.report_done)
def do_reports(self, *args):
# note this makes a new widget with every click ... this is bad. Refactor to reuse the widget.
self.pbar = ttk.Progressbar(self.mainframe, orient = HORIZONTAL, mode = 'indeterminate')
self.pbar.grid(row = 4, column = 3, sticky = (W, E))
start, end = 4,5
t1 = threading.Thread(target = run_report, args = [root, start, end])
t1.start()
self.pbar.start()
def report_done(self, event=None):
self.pbar.stop()
Label(self.mainframe, text="report done").grid(row = 4, column = 3)
root = Tk()
BillingApp(root)
root.mainloop()
I see that you have accepted the answer above. However, I would post my answer since it is not based on threading, which I think is simpler and may be more suitable:
from tkinter import *
from tkinter.ttk import *
import os
root = Tk()
# I set the length and maximum as shown to demonstrate the process in the
# proceeding function
progress = Progressbar(root, orient = HORIZONTAL,
length = 200/5, maximum=200/5, mode = 'determinate')
# Function
def my_func():
t=0
r= 1/5
for i in range(200):
print(i)
t=t+r
progress['value'] = t
root.update_idletasks()
progress.pack()
# Button
Button(root, text = 'Start', command = bar).pack(pady = 10)
mainloop()

Tkinter pyowm not updating

I have several widgets, or more correctly, Labels, that update perfectly when a method finishing with self.after() is called in __init__.
That's not the case with this Label here, which gets the weather Information from OWM (OpenWeatherMap) through pyowm, and should be updated every specific amount of time, defined in the after() function.
I've tried almost everything in my knowledge, though I'm pretty newbie to Python. I've searched google for days, but no working solution was found, or not that I could make it work.
I've tried to put the after function in every method, even __init__.
The trimmed down main() for debug and the Weather Class follows:
import tkinter as tk
from Weather import Meteo
def displayMeteo(win):
# Place Meteo portlet
meteoHandler = Meteo(win, bg='black', fg='white')
meteoHandler.pack(side='top', anchor='ne')
def main():
# Set the screen definition, root window initialization
root = tk.Tk()
root.configure(background='black')
width, height = root.winfo_screenwidth(),
root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (width, height))
label = tk.Label(root, text="Monitor Dashboard", bg='black',
fg='red')
label.pack(side='bottom', fill='x', anchor='se')
# Display portlet
displayMeteo(root)
# Loop the GUI manager
root.mainloop(0)
###############################
# MAIN SCRIPT BODY PART #
###############################
if __name__ == "__main__":
main()
And the class:
import pyowm
import tkinter as tk
from PIL import Image, ImageTk
class Meteo(tk.Label):
def __init__(self, parent=None, **params):
tk.Label.__init__(self, parent)
self.API = pyowm.OWM('API KEY', config_module=None,
language='it', subscription_type=None)
self.location = self.API.weather_at_place('Rome,IT')
self.weatherdata = self.location.get_weather()
self.weather = str(self.weatherdata.get_detailed_status())
self.dictionary = {'poche nuvole': 'Parzialmente Nuvoloso',
'cielo sereno': 'Sereno', 'nubi sparse': 'Nuvoloso',
'temporale con pioggia': 'Temporale', 'pioggia leggera':
'Pioggerella'}
self.Display()
def Temperatur(self):
self.Temp = tk.StringVar()
self.tempvalue = self.weatherdata.get_temperature('celsius')
self.temperature = str(self.tempvalue.get('temp'))
self.Temp.set(self.temperature)
self.after(3000, self.Temperatur)
def WeatherInfo(self):
self.Weather = tk.StringVar()
self.weather = str(self.weatherdata.get_detailed_status())
if self.weather in self.dictionary:
self.weather = self.dictionary[self.weather]
self.weatherstring = self.weather.title()
self.Weather.set(self.weatherstring)
self.after(3000, self.WeatherInfo)
def Display(self):
self.Temperatur()
self.WeatherInfo()
self.configure(text=self.Weather.get() + ", " + self.Temp.get() + "°", bg='black',
fg='#FFFF96', font=("arial, 35"))
self.after(3000, self.Display)
Now, what should happen is the widget updating every 3 secs (just to debug), although I know even if updating works it won't change every 3 seconds, 'cause you know...weather doesn't change every 3 secs.
As done in suggestion of Idlehands the problem was not in the Label or the updating.
The Code, if written that way, would call .get_weather only once, thus creating a stagnating variable. I added a method that updates the pyowm parsing and now evertyhing's working fine!

Tkinter Text Window, update on open and close

I am trying to create a Tkinter app, where when you press a button, a new Window opens which has continually updating text about certain parts of the program. My problem is the section of code where I am trying to add the text to the screen. This is what I have written:
import tkinter as tk
import time
class TextWindow(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.textArea = tk.Text(self, height = 10, width = 30)
self.textArea.pack(side = "left", fill = "y")
bar = tk.Scrollbar(self)
bar.pack(side = "right", fill = "y")
bar.config(command = self.textArea.yview)
def output(self, value):
outputVal = str(value)
self.textArea.inser('end', "{0}\n".format(outputVal))
self.textArea.see('end')
def openWindow():
textWindow = tk.Toplevel(root)
textFrame = TextWindow(textWindow)
textFrame.pack()
value = 0.0
alive = True
while alive:
if textWindow.winfo_exists:
value = value + 0.1
textFrame.output(value)
time.sleep(0.1)
else:
alive = False
root = tk.Tk
btn = tk.Button(root, text = "Click", command = openWindow)
btn.pack()
root.mainloop()
When I comment out the while loop in the openWindow method, the window opens and closes, and reopens, no problem. However when the code is there, I never see the window when I press the button.
I tried running it through the IDLE debugger, and I am not getting any errors, and everything runs through the loop fine, however the Window still never appears. What is my problem?
The answer that Jason S gave is not a good example. You can avoid any issues with sleep by just using after() instead. Don't settle for "Kinda works".
Here is a break down of how you could accomplish what you need without having the problems associated with sleep() and tkinter.
First you are importing Tk() wrong. Don't do tk.Tk do tk.Tk()
Now lets move the entire program into a single class. This will provide us with the ability to use class attributes and make things a bit easier to work with.
Here we create a class called guiapp(tk.Frame): you can name it what you want but this is just my example. Then make sure you are passing root using guiapp(root) so we can work in this class on the tk.Tk() instance. This will be shown at the bottom of the program where the class is instantiated.
Because we have passed root to the class we can place the button that opens the Toplevel window on our self.master attribute.
UPDATE: Changed how data is sent to the Textbox in Toplevel so we can retain the information in case you want to reopen top level. per your comment.
import tkinter as tk
class guiapp(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.master = master
self.value = 0.0
self.alive = True
self.list_for_toplevel = [] # added list to retain values for Toplevel
btn = tk.Button(self.master, text = "Click", command = self.TextWindow)
btn.pack()
Here we add the method to define the Topelevel we are going to create.
Because everything is inside this one class we can create this Topelevel as a Toplevel of self.master. At the end of this method we call the self.timed_loop() method I added that manages the timed portion of your program. UPDATE: added a call to a new function.
def TextWindow(self):
self.textWindow = tk.Toplevel(self.master)
self.textFrame = tk.Frame(self.textWindow)
self.textFrame.pack()
self.textArea = tk.Text(self.textWindow, height = 10, width = 30)
self.textArea.pack(side = "left", fill = "y")
bar = tk.Scrollbar(self.textWindow)
bar.pack(side = "right", fill = "y")
bar.config(command = self.textArea.yview)
self.alive = True
self.add_list_first()
UPDATE: Added a new function called add_list_first(self):. This will allow us to first add any values that are stored in the list then we can call timed_loop() to continue appending the list and counting.
def add_list_first(self):
for item in self.list_for_toplevel:
self.textArea.insert('end', "{}\n".format(item))
self.textArea.see('end')
self.timed_loop()
Here we have created a method to perform the task you have in you code for the Toplevel that uses the after() function from tkinter. ever 1000 is equal to 1 second, so play with that timer if you want. The first part of after() is for the time in milliseconds and the 2nd part is the function being called. In this case it calls itself to continue the loop until either the Toplevel window self.textWindow is closed or the self.alive variable is no longer True.
UPDATE: I have added a for loop to insert the list instead of directly imputing each value. This way we can retain the data if we want to reopen the Toplevel.
def timed_loop(self):
if self.alive == True and tk.Toplevel.winfo_exists(self.textWindow):
self.master.after(1000, self.timed_loop)
self.value += 1
self.list_for_toplevel.append(self.value)
self.textArea.delete(1.0, "end-1c")
for item in self.list_for_toplevel:
self.textArea.insert('end', "{}\n".format(item))
self.textArea.see('end')
else:
self.alive = False
This is the preferred way to start your class going in tkinter. As you can see we have created root as tk.Tk() and passed root into the the class guiapp(). Also note that I assigned this instance of the class to the variable name myapp. This will allow us to interact with the class from outside of the class if you ever need to. It does not make a difference in this case but I thought I would add it just the same.
if __name__ == "__main__":
root = tk.Tk()
myapp = guiapp(root)
root.mainloop()
Here is the copy paste version for you to use.
import tkinter as tk
class guiapp(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.master = master
self.value = 0.0
self.alive = True
self.list_for_toplevel = []
btn = tk.Button(self.master, text = "Click", command = self.TextWindow)
btn.pack()
def TextWindow(self):
self.textWindow = tk.Toplevel(self.master)
self.textFrame = tk.Frame(self.textWindow)
self.textFrame.pack()
self.textArea = tk.Text(self.textWindow, height = 10, width = 30)
self.textArea.pack(side = "left", fill = "y")
bar = tk.Scrollbar(self.textWindow)
bar.pack(side = "right", fill = "y")
bar.config(command = self.textArea.yview)
self.alive = True
self.add_list_first()
def add_list_first(self):
for item in self.list_for_toplevel:
self.textArea.insert('end', "{}\n".format(item))
self.textArea.see('end')
self.timed_loop()
def timed_loop(self):
if self.alive == True and tk.Toplevel.winfo_exists(self.textWindow):
self.master.after(1000, self.timed_loop)
self.value += 1
self.list_for_toplevel.append(self.value)
outputVal = str(self.value)
self.textArea.insert('end', "{0}\n".format(outputVal))
self.textArea.see('end')
else:
self.alive = False
if __name__ == "__main__":
root = tk.Tk()
myapp = guiapp(root)
root.mainloop()
The problem is that you are never giving control back to the Tkinter main loop. The code gets stuck executing in the while loop, meaning Tkinter never gets to refresh the display or process any other events. You could force update by calling root.update() right before time.sleep(0.1), but this is not really optimal and the display will be unresponsive while sleeping. Depending on what you are doing, it may be good enough.
See here for additional explanation

GUI programming using Tkinter Python

I have a Tkinter GUI having 2 entry fields, 2 buttons ( initialization of these not shown in code). There is one more button (initialized in code) which performs the main task of performing change detection on two images. Also there is a progress bar.
Now, when the task of change detection has been completed, I want to display the 4 images(pre, post, aligned, chng) returned by wave.changedetection() in a separate Tkinter window. I want the new window to come only after changedetection() has completed.(wave.py is my own file, not some module)
Unfortunately, if I try to add code to make new window, Tk.Toplevel() ,after the wave.changedetection() call, nothing happens and the main GUI window becomes unresponsive and has to be killed.
There is no way to know when the new created thread (start_thread)completes it's work, so that I can do Tk.Toplevel() there.
How can I do what I require?
class GUI(Tkinter.Tk):
def __init__(self, parent)
Tkinter.Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.button = Tkinter.Button(text = "Start")
self.button.bind('<Button-1>', self.OnButtonClick)
self.button.pack()
self.int = Tkinter.IntVar()
self.pgbar = Tkinter.ProgressBar(variable = self.int, mode = determinate)
def OnButtonClick(self,event):
#this func has been made since I have more buttons.It may seem redundant here
self.button['command'] = self.start_thread()
self.update_idletasks()
def start_thread(self):
self.int_var.set(1)
q = queue.Queue()
self.secondary_thread = threading.Thread(target = self.change)
self.secondary_thread.start()
self.after(50, self.check_queue, q)
def check_queue(self, q):
while True:
try:
x = wave.q.get_nowait()
except queue.Empty :
self.after(50,self.check_queue,q)
break
else:
self.int_var.set(x)
if x == 6:
self.button3['state'] = 'normal'
break
def change(self):
'''The 6 functions of wave.changedetection() change the value of self.int
due to which progress bar progresses.'''
pre, post, aligned, chng = wave.changedetection(self.entry_1.get(),
self.entry_2.get())
if __name__ == '__main__':
gui = GUI(None)
gui.mainloop()
code to update progress bar taken from here (2nd answer,Honest Abe's answer)
You have to be able to differentiate name spaces, i.e. this is in the main window and this is in the Toplevel. I would suggest that you get the Toplevels working first and then decide if you want to add threading or not. The code below is a simple example of creating Toplevels and shows how to place widgets in a specific name space (window in this case). You may or may not want a separate "create a Toplevel" class if there are functions you want to associate with each Toplevel's namespace. Also there are examples on the web on using Tkinter's "after" to update a progressbar. That is a different question so start another thread if you have questions about the progressbar.
try:
import Tkinter as tk ## Python 2.x
except ImportError:
import tkinter as tk ## Python 3.x
from functools import partial
class OpenToplevels():
""" open and close additional Toplevels with a button
"""
def __init__(self):
self.root = tk.Tk()
self.button_ctr=0
## in the "root" namespace *********************
but=tk.Button(self.root, text="Open a Toplevel",
command=self.open_another)
but.grid(row=0, column=0)
tk.Button(self.root, text="Exit Tkinter", bg="red",
command=self.root.quit).grid(row=1, column=0, sticky="we")
self.root.mainloop()
def close_it(self, id):
## destroy the window in this id's namespace ***********
id.destroy()
## id.withdraw()
## id.iconify()
def open_another(self):
self.button_ctr += 1
id = tk.Toplevel(self.root)
id.title("Toplevel #%d" % (self.button_ctr))
## in the "id for this Toplevel" namespace ***********
tk.Button(id, text="Close Toplevel #%d" % (self.button_ctr),
command=partial(self.close_it, id),
bg="orange", width=20).grid(row=1, column=0)
Ot=OpenToplevels()

How can I update a Tkinter canvas in the middle of a function call?

I'm writing Conway's game of life using Tkinter, and I want to have a "Go" button which allows the animation to begin, and continue stepping automatically until terminated. I'm using a Canvas to draw the environment, but since the "Go" button needs to complete the function call before it updates the canvas, the window just hangs until I kill the process. I've attempted to use canvas.update_idletasks() and canvas.update() (followed by a few seconds of sleeping) at points where I want to updated the canvas but that doesn't seem to do the trick. Any Ideas? Below is my GameOfLife class, the Environment class just manages the "board" of cells.
from Tkinter import *
from random import *
from time import time
from Environment import *
class GameOfLife(object):
def __init__(self, master, envDim):
self.unitSize = 10
self.dimension = envDim * self.unitSize
self.environment = Environment(envDim)
self.environment.seedBoard()
self.started = False
frame = Frame(master)
frame.pack()
Button(frame, text = "Go", command = self.go_call).pack(side = LEFT)
Button(frame, text = "Clear", command = self.reset_call).pack(side = LEFT)
Button(frame, text = "Close", command = frame.quit).pack(side = RIGHT)
canvas = self.drawCanvas(master, self.dimension)
def drawCanvas(self, master, dimension):
self.canvas = Canvas(master, width = self.dimension, height = self.dimension)
self.canvas.pack()
return self.canvas
def go_call(self):
print "<< Go Call >>"
if self.environment.started == False:
self.environment.seedBoard()
self.drawState(self.environment)
self.environment.nextBoard()
self.started = True
while True:
self.environment.nextBoard()
self.canvas.delete(ALL)
self.drawState(self.environment)
self.canvas.update_idletasks()
sleep(4)
def reset_call(self):
print "<< Reset Call >>"
self.canvas.delete(ALL)
self.environment = Environment(self.environment.dim)
def drawState(self, environment):
size = self.unitSize
for x in range(environment.dim):
for y in range(environment.dim):
if environment.matrix[x][y].alive == True:
xs = x * size
ys = y * size
self.canvas.create_rectangle(xs, ys, xs+size, ys+size, fill = 'black')
envDim = 70
root = Tk()
gol = GameOfLife(root, envDim)
root.mainloop()
You should never put an infinite loop inside your GUI program, and you definitely should never, ever call sleep. You already have an infinite loop running -- the event loop -- so take advantage of it.
The way to do animations like this is to write a function that draws one frame of the animation (or turn of the game, pick your metaphor). Then, have that function call itself via after.
Roughly speaking your code should look like this:
def draw(self):
# draw the board according to the current state
...
# arrange for the next frame to draw in 4 seconds
self.after(4000, self.draw)
def __init__(self, ...):
...
self.go = tk.Button(self, text="Go", command=self.draw)
...
If you want to add a stop button, all it needs to do is set a flag. Then, in draw, simply check for the flag before calling self.after

Categories