There's no help on google, I've asked some people as well but none of them seem to know how to answer my question.
I'm programming a GUI for a project, and it contains an RSS-Feed ticker.
It scrolls through the news and when it updates (every 3 seconds for obvious debug reasons) it speeds up a bit.
This means, if I run the program, after two hours the ticker is scrolling at a non-human readable speed.
The main code wasn't written by me, I modified it and added the update function.
main():
import tkinter as tk
from Press import RSSTicker
def displayRSSticker(win):
# Place RSSTicker portlet
tickerHandle = RSSTicker(win, bg='black', fg='white', highlightthickness=0, font=("avenir", 30))
tickerHandle.pack(side='bottom', fill='x', anchor='se')
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
displayRSSticker(root)
# Loop the GUI manager
root.mainloop(0)
###############################
# MAIN SCRIPT BODY PART #
###############################
if __name__ == "__main__":
main()
RSSTicker class:
import feedparser
import tkinter as tk
class RSSTicker(tk.Text):
# Class constructor
def __init__(self, parent, **params):
super().__init__(parent, height=1, wrap="none", state='disabled', **params)
self.newsFeed = feedparser.parse('http://www.repubblica.it/rss/homepage/rss2.0.xml')
self.update()
# Class methods
def update(self):
self.headlineIndex = 0
self.text = ''
self.pos = 0
self.after_idle(self.updateHeadline)
self.after_idle(self.scroll)
self.after(4000, self.update)
def updateHeadline(self):
try:
self.text += ' ' + self.newsFeed['entries'][self.headlineIndex]['title']
except IndexError:
self.headlineIndex = 0
self.text = self.feed['entries'][self.headlineIndex]['title']
self.headlineIndex += 1
self.after(5000, self.updateHeadline)
def scroll(self):
self.config(state='normal')
if self.pos < len(self.text):
self.insert('end', self.text[self.pos])
self.pos += 1
self.see('end')
self.config(state='disabled')
self.after(180, self.scroll)
I thought the problem lied in the self.pos variable, printing it out resulted in it counting up, resetting to 1 and counting up faster.. But it doesn't seem to be problem causing the acceleration of the ticker.
From what I've understood tho, the problem must be in the scroll method.
If someone understand how to keep the original scroll speed when updated, thank you.
I think you can use a couple tracking variables to make sure that update only starts the loop once and then next time update is called it will just run scroll without starting a new loop. At the same time if scroll is not called by update then it will continue looping as needed.
Change your RSSTicker class to the following:
class RSSTicker(tk.Text):
# Class constructor
def __init__(self, parent, **params):
self.scroll_started = False # Tracker for first update.
super().__init__(parent, height=1, wrap="none", state='disabled', **params)
self.newsFeed = feedparser.parse('http://www.repubblica.it/rss/homepage/rss2.0.xml')
self.update()
def update(self):
self.headlineIndex = 0
self.text = ''
self.pos = 0
self.after_idle(self.updateHeadline)
self.after_idle(lambda: self.scroll('update'))
self.after(4000, self.update)
def updateHeadline(self):
try:
self.text += ' ' + self.newsFeed['entries'][self.headlineIndex]['title']
except IndexError:
self.headlineIndex = 0
self.text = self.feed['entries'][self.headlineIndex]['title']
self.headlineIndex += 1
self.after(5000, self.updateHeadline)
def scroll(self, from_after_or_update = 'after'):
self.config(state='normal')
if self.pos < len(self.text):
self.insert('end', self.text[self.pos])
self.pos += 1
self.see('end')
self.config(state='disabled')
# Check if the loop started by after.
if from_after_or_update != 'update':
self.scroll_started = True
self.after(180, self.scroll)
# If not started by after check to see if it is the 1st time loop is started by "update".
elif self.scroll_started is False and from_after_or_update == 'update':
self.scroll_started = True
self.after(180, self.scroll)
# If neither of the above conditions then do not start loop to prevent multiple loops.
else:
print("ran scroll method without adding new after loop!")
Related
This is a function that creates an image:
def footgraph(self):
load = Image.open('centertext_out.png')
load= load.resize((500, 500), Image.ANTIALIAS)
render = ImageTk.PhotoImage(load)
self.img = Label( image=render)
self.img.image = render
self.img.place(x=150, y=5)
self.scale = tk.Scale(self.win, variable=self.value, orient="horizontal",length = 200,
from_=self.df['index'].unique().min(), to=self.df['index'].unique().max(), resolution =1,command=self.updateScaleFoot)
self.scale.place(x=250, y = 500)
self.stop.pack(expand='true',fill='both')
self.stop.place(x=200, y =500)
self.play.pack(expand='true',fill='both')
self.play.place(x=150, y = 500)
Here is a code for play button that keeps updating image:
while True:
self.index += 1
#update image(centertext_out.png) and save
load = Image.open('centertext_out.png')
load= load.resize((500, 500), Image.ANTIALIAS)
render = ImageTk.PhotoImage(load)
img = Label( image=render)
img.image = render
img.place(x=150, y=5)
time.sleep(10)
This loop is working fine. Self.index keeps updating. But image is not updating and screen hangs.
Edit:
When I use slider new image gets appended to previous one like this :
Edit:
I have narrowed down the problem. Below is the code for play function. When I click bar graph and if for bar graph turns true then code runs smoothly but it doesnt seem to be working for the second statement even if I have nothing inside of it.
def startanimation(self):
self.pause = 0
print("pause"+ str(self.pause))
while True:
if self.pause == 1:
break
self.index = self.index +1
print ("scale is now %s" % (self.index))
if "bar" in self.graphtype:
#some code
#draw canvas
self.fig.canvas.draw()
self.fig.canvas.flush_events()
time.sleep(0.2)
if "foot" in self.graphtype:
print("inside")
time.sleep(0.2)
Edit:
Changed code according to one of the answers.
class Application:
def __init__(self, master):
self.win = master
self.geo = self.win.geometry
self.geo("800x800+400+400")
self.win['bg'] = "black"
####################################################some code
def startanimation(self):
self.pause = 0
print("pause"+ str(self.pause))
if "foot" in self.graphtype:
self.win.after(1,self.test)
def test(self):
print("hi")
self.pause = 0
guide = pd.read_csv("guide.csv")
print("hey")
self.index +=1
test is called only once. hey is printed only once
Solved: check this link
Python Tkinter after() Only Executing Once
I am trying to create stopwatch. I have done it but I would like to pause and continue the time whenever I want. I have tried some things but I have no idea how to do it. Is there anybody who would explain me how to do it?
import time, tkinter
canvas=tkinter.Canvas(width=1900,height=1000,bg='white')
canvas.pack()
canvas.create_text(950,300,text=':',font='Arial 600')
def write(x_rec,y_rec,x_text,rep):
canvas.create_rectangle(x_rec,0,y_rec,750,outline='white',fill='white')
if rep<10:
canvas.create_text(x_text,400,text='0'+str(rep),font='Arial 600')
else:
canvas.create_text(x_text,400,text=str(rep),font='Arial 600')
def write_minutes(rep):
write(0,900,450,rep)
def write_seconds(rep):
write(1000,1900,1450,rep)
def time(num,remember):
while remember[0]<num:
remember[1]+=1
write_seconds(remember[1])
if remember[1]==60:
remember[0]+=1
remember[1]=0
write_seconds(remember[1])
write_minutes(remember[0])
canvas.update()
canvas.after(1000)
remember=[0,0]
num=1
write_seconds(remember[1])
write_minutes(remember[0])
time(5,remember)
I couldn't figure-out a clean way to modify your code to do what you want, so decided to implement the stop watch as a class to make the program more object-oriented and avoid the use of a bunch of global variables.
I haven't tested this thoroughly, but there's enough of it working to give you the idea. Note also that I changed a Resume button into one that toggles itself between that and being Pause button. This approach made adding a third one unnecessary.
Update
I noticed what could be potential problem because more and more objects keep getting added to the Canvas as the display is updated. This shouldn't be a problem for a short-running StopWatch instance, but might cause issues with a long-running one.
To avoid this, I modified the code to update the existing corresponding Canvas text object if there is one. I also moved the Buttons to the top, above the StopWatch.
from functools import partial
import time
import tkinter as tk
PAUSE, RESUME = 0, 1 # Button states.
# Button callback functions.
def _toggle(callback):
toggle_btn.state = 1 - toggle_btn.state # Toggle button state value.
toggle_btn.config(**toggle_btn_states[toggle_btn.state])
callback()
def _stop():
stopwatch.cancel_updates()
toggle_btn.config(state=tk.DISABLED)
stop_btn.config(state=tk.DISABLED)
class StopWatch:
def __init__(self, parent, run_time, width, height):
self.run_time = run_time
self.width, self.height = width, height
self.font = 'Arial 600'
self.canvas = tk.Canvas(parent, width=width, height=height, bg='white')
self.canvas.pack()
self.canvas.create_text(950, 300, text=':', font=self.font)
self.running, self.paused = False, False
self.after_id = None
def start(self):
self.elapsed_time = 0 # In seconds.
self._display_time()
self.after_id = self.canvas.after(1000, self._update)
self.running, self.paused = True, False
def _update(self):
if self.running and not self.paused:
if self.elapsed_time == self.run_time:
_stop() # Sets self.running to False.
self.canvas.bell() # Beep.
else:
self.elapsed_time += 1
self._display_time()
if self.running: # Keep update process going.
self.after_id = self.canvas.after(1000, self._update)
def _display_time(self):
mins, secs = divmod(self.elapsed_time, 60)
self._write_seconds(secs)
self._write_minutes(mins)
def _write_minutes(self, mins):
self._write(0, 900, 450, 'mins', mins)
def _write_seconds(self, secs):
self._write(1000, 1900, 1450, 'secs', secs)
def _write(self, x_rec, y_rec, x_text, tag, value):
text = '%02d' % value
# Update canvas text widget if it has non-empty text.
if self.canvas.itemcget(tag, 'text'):
self.canvas.itemconfigure(tag, text=text)
else: # Otherwise create it.
self.canvas.create_text(x_text, 400, text=text, tag=tag, font=self.font)
def pause_updates(self):
if self.running:
self.paused = True
def resume_updates(self):
if self.paused:
self.paused = False
def cancel_updates(self):
self.running, self.paused = False, False
if self.after_id:
self.canvas.after_cancel(self.after_id)
self.after_id = None
# main
root = tk.Tk()
# Create a Frame for Buttons (allows row of them to be centered).
button_frame = tk.Frame(root)
button_frame.pack(side=tk.TOP)
# Create StopWatch and configure buttons to use it.
stopwatch = StopWatch(root, 5, 1900, 1000)
toggle_btn = tk.Button(button_frame)
toggle_btn_states = {}
# Dictionary mapping state to button configuration.
toggle_btn_states.update({
PAUSE: dict(
text='Pause', bg='red', fg='white',
command=partial(_toggle, stopwatch.pause_updates)),
RESUME: dict(
text='Resume', bg='green', fg='white',
command=partial(_toggle, stopwatch.resume_updates))
})
toggle_btn.state = PAUSE
toggle_btn.config(**toggle_btn_states[toggle_btn.state])
toggle_btn.pack(side=tk.LEFT, padx=2)
stop_btn = tk.Button(button_frame, text='Stop', bg='blue', fg='white', command=_stop)
stop_btn.pack(side=tk.LEFT, padx=2)
stopwatch.start()
root.mainloop()
Here's a screenshot showing the stopwatch running:
So I have been playing with tkinter to try add a gui to a lift simulator project I have written for university. It is not really needed, but I would like to add it.
Here is the code that I currently have.
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
master.title("Test") #Controls the window title.
self.pack()
self.createWidgets()
def createWidgets(self):
floors = [i for i in range(41)]
buttons = []
xPos = 0
yPos = 0
for floor in floors:
if(yPos == 5):
xPos = xPos + 1
yPos = 0
if(xPos == 8):
yPos = 2
self.button = tk.Button(self, width=3, text=floor,
command = lambda f=floor: self.pressed(f))
self.button.grid(row=xPos, column =yPos)
yPos = yPos +1
self.QUIT = tk.Button(self, text="QUIT", fg="red",
command=root.destroy).grid(row = xPos, column = yPos)
def pressed(self, index):
print("number pressed", index)
self.button.configure(bg = "red")
root = tk.Tk()
app = Application(master=root)
app.mainloop()
This is all fine and dandy other than when the button is pressed it prints out the correct number, but it changes the background of the last button (number 40) to red, not the one pressed.
If you could let me know what needs correcting that would be great.
Thanks
self.button can only ever reference a single button, and it will always be whatever was assigned to it last. A simple solution is to store the button references in a dict, using floor as the key. Since you're passing that to the callback, you then have everything you need to reconfigure the button:
def createWidgets(self):
...
self.buttons = {}
for floor in floors:
...
self.buttons[floor] = tk.Button(...)
...
def pressed(self, index):
...
self.buttons[index].configure(bg="red")
I have a GUI made with TKinter in Python. I would like to be able to display a message when my mouse cursor goes, for example, on top of a label or button. The purpose of this is to explain to the user what the button/label does or represents.
Is there a way to display text when hovering over a tkinter object in Python?
I think this would meet your requirements.
Here's what the output looks like:
First, A class named ToolTip which has methods showtip and hidetip is defined as follows:
from tkinter import *
class ToolTip(object):
def __init__(self, widget):
self.widget = widget
self.tipwindow = None
self.id = None
self.x = self.y = 0
def showtip(self, text):
"Display text in tooltip window"
self.text = text
if self.tipwindow or not self.text:
return
x, y, cx, cy = self.widget.bbox("insert")
x = x + self.widget.winfo_rootx() + 57
y = y + cy + self.widget.winfo_rooty() +27
self.tipwindow = tw = Toplevel(self.widget)
tw.wm_overrideredirect(1)
tw.wm_geometry("+%d+%d" % (x, y))
label = Label(tw, text=self.text, justify=LEFT,
background="#ffffe0", relief=SOLID, borderwidth=1,
font=("tahoma", "8", "normal"))
label.pack(ipadx=1)
def hidetip(self):
tw = self.tipwindow
self.tipwindow = None
if tw:
tw.destroy()
def CreateToolTip(widget, text):
toolTip = ToolTip(widget)
def enter(event):
toolTip.showtip(text)
def leave(event):
toolTip.hidetip()
widget.bind('<Enter>', enter)
widget.bind('<Leave>', leave)
The widget is where you want to add the tip. For example, if you want the tip when you hover over a button or entry or label, the instance of the same should be provided at the call time.
Quick note: the code above uses from tkinter import *
which is not suggested by some of the programmers out there, and they have valid points. You might want to make necessary changes in such case.
To move the tip to your desired location, you can change x and y in the code.
The function CreateToolTip() helps to create this tip easily. Just pass the widget and string you want to display in the tipbox to this function, and you're good to go.
This is how you call the above part:
button = Button(root, text = 'click mem')
button.pack()
CreateToolTip(button, text = 'Hello World\n'
'This is how tip looks like.'
'Best part is, it\'s not a menu.\n'
'Purely tipbox.')
Do not forget to import the module if you save the previous outline in different python file, and don't save the file as CreateToolTip or ToolTip to avoid confusion.
This post from Fuzzyman shares some similar thoughts, and worth checking out.
You need to set a binding on the <Enter> and <Leave> events.
Note: if you choose to pop up a window (ie: a tooltip) make sure you don't pop it up directly under the mouse. What will happen is that it will cause a leave event to fire because the cursor leaves the label and enters the popup. Then, your leave handler will dismiss the window, your cursor will enter the label, which causes an enter event, which pops up the window, which causes a leave event, which dismisses the window, which causes an enter event, ... ad infinitum.
For simplicity, here's an example that updates a label, similar to a statusbar that some apps use. Creating a tooltip or some other way of displaying the information still starts with the same core technique of binding to <Enter> and <Leave>.
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.l1 = tk.Label(self, text="Hover over me")
self.l2 = tk.Label(self, text="", width=40)
self.l1.pack(side="top")
self.l2.pack(side="top", fill="x")
self.l1.bind("<Enter>", self.on_enter)
self.l1.bind("<Leave>", self.on_leave)
def on_enter(self, event):
self.l2.configure(text="Hello world")
def on_leave(self, enter):
self.l2.configure(text="")
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(side="top", fill="both", expand="true")
root.mainloop()
You can refer to this- HoverClass
It is exactly what you need. Nothing more, nothing less
from Tkinter import *
import re
class HoverInfo(Menu):
def __init__(self, parent, text, command=None):
self._com = command
Menu.__init__(self,parent, tearoff=0)
if not isinstance(text, str):
raise TypeError('Trying to initialise a Hover Menu with a non string type: ' + text.__class__.__name__)
toktext=re.split('\n', text)
for t in toktext:
self.add_command(label = t)
self._displayed=False
self.master.bind("<Enter>",self.Display )
self.master.bind("<Leave>",self.Remove )
def __del__(self):
self.master.unbind("<Enter>")
self.master.unbind("<Leave>")
def Display(self,event):
if not self._displayed:
self._displayed=True
self.post(event.x_root, event.y_root)
if self._com != None:
self.master.unbind_all("<Return>")
self.master.bind_all("<Return>", self.Click)
def Remove(self, event):
if self._displayed:
self._displayed=False
self.unpost()
if self._com != None:
self.unbind_all("<Return>")
def Click(self, event):
self._com()
Example app using HoverInfo:
from Tkinter import *
from HoverInfo import HoverInfo
class MyApp(Frame):
def __init__(self, parent=None):
Frame.__init__(self, parent)
self.grid()
self.lbl = Label(self, text='testing')
self.lbl.grid()
self.hover = HoverInfo(self, 'while hovering press return \n for an exciting msg', self.HelloWorld)
def HelloWorld(self):
print('Hello World')
app = MyApp()
app.master.title('test')
app.mainloop()
Screenshot:
I have a very hacky solution but it has some advantages over the current answers so I figured I would share it.
lab=Label(root,text="hover me")
lab.bind("<Enter>",popup)
def do_popup(event):
# display the popup menu
root.after(1000, self.check)
popup = Menu(root, tearoff=0)
popup.add_command(label="Next")
popup.tk_popup(event.x_root, event.y_root, 0)
def check(event=None):
x, y = root.winfo_pointerxy()
widget = root.winfo_containing(x, y)
if widget is None:
root.after(100, root.check)
else:
leave()
def leave():
popup.delete(0, END)
The only real issue with this is it leaves behind a small box that moves focus away from the main window
If anyone knows how to solve these issues let me know
If anyone is on Mac OSX and tool tip isn't working, check out the example in:
https://github.com/python/cpython/blob/master/Lib/idlelib/tooltip.py
Basically, the two lines that made it work for me on Mac OSX were:
tw.update_idletasks() # Needed on MacOS -- see #34275.
tw.lift() # work around bug in Tk 8.5.18+ (issue #24570)
Here is a simple solution to your problem that subclasses the tk.Button object. We make a special class that tk.Button inherits from, giving it tooltip functionality. The same for tk.Labels.
I don't know what would be cleanest and the easiest way to maintain code for keeping track of the text that goes into the tooltips. I present here one way, in which I pass unique widget IDs to MyButtons, and access a dictionary for storing the tooltip texts. You could store this file as a JSON, or as a class attribute, or as a global variable (as below). Alternatively, perhaps it would be better to define a setter method in MyButton, and just call this method every time you create a new widget that should have a tooltip. Although you would have to store the widget instance in a variable, adding one extra line for all widgets to include.
One drawback in the code below is that the self.master.master syntax relies on determining the "widget depth". A simple recursive function will catch most cases (only needed for entering a widget, since by definition you leave somewhere you once were).
Anyway, below is a working MWE for anyone interested.
import tkinter as tk
tooltips = {
'button_hello': 'Print a greeting message',
'button_quit': 'Quit the program',
'button_insult': 'Print an insult',
'idle': 'Hover over button for help',
'error': 'Widget ID not valid'
}
class ToolTipFunctionality:
def __init__(self, wid):
self.wid = wid
self.widet_depth = 1
self.widget_search_depth = 10
self.bind('<Enter>', lambda event, i=1: self.on_enter(event, i))
self.bind('<Leave>', lambda event: self.on_leave(event))
def on_enter(self, event, i):
if i > self.widget_search_depth:
return
try:
cmd = f'self{".master"*i}.show_tooltip(self.wid)'
eval(cmd)
self.widget_depth = i
except AttributeError:
return self.on_enter(event, i+1)
def on_leave(self, event):
cmd = f'self{".master" * self.widget_depth}.hide_tooltip()'
eval(cmd)
class MyButton(tk.Button, ToolTipFunctionality):
def __init__(self, parent, wid, **kwargs):
tk.Button.__init__(self, parent, **kwargs)
ToolTipFunctionality.__init__(self, wid)
class MyLabel(tk.Label, ToolTipFunctionality):
def __init__(self, parent, wid, **kwargs):
tk.Label.__init__(self, parent, **kwargs)
ToolTipFunctionality.__init__(self, wid)
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.tooltip = tk.StringVar()
self.tooltip.set(tooltips['idle'])
self.frame = tk.Frame(self, width=50)
self.frame.pack(expand=True)
MyLabel(self.frame, '', text='One Cool Program').pack()
self.subframe = tk.Frame(self.frame, width=40)
self.subframe.pack()
MyButton(self.subframe, 'button_hello', text='Hello!', command=self.greet, width=20).pack()
MyButton(self.subframe, 'button_insutl', text='Insult', command=self.insult, width=20).pack()
MyButton(self.subframe, 'button_quit', text='Quit', command=self.destroy, width=20).pack()
tk.Label(self.subframe, textvar=self.tooltip, width=20).pack()
def show_tooltip(self, wid):
try:
self.tooltip.set(tooltips[wid])
except KeyError:
self.tooltip.set(tooltips['error'])
def hide_tooltip(self):
self.tooltip.set(tooltips['idle'])
def greet(self):
print('Welcome, Fine Sir!')
def insult(self):
print('You must be dead from the neck up')
if __name__ == '__main__':
app = Application()
app.mainloop()
The best way I have found to create a popup help window is to use the tix.Balloon. I have modified it below to make it look better and show an example (note the use of tix.Tk):
import tkinter as tk
import tkinter.tix as tix
class Balloon(tix.Balloon):
# A modified tix popup balloon (to change the default delay, bg and wraplength)
init_after = 1250 # Milliseconds
wraplength = 300 # Pixels
def __init__(self, master):
bg = root.cget("bg")
# Call the parent
super().__init__(master, initwait=self.init_after)
# Change background colour
for i in self.subwidgets_all():
i.config(bg=bg)
# Modify the balloon label
self.message.config(wraplength=self.wraplength)
root = tix.Tk()
l = tk.Label(root, text="\n".join(["text"] * 5))
l.pack()
b = Balloon(root.winfo_toplevel())
b.bind_widget(l, balloonmsg="Some random text")
root.mainloop()
OLD ANSWER:
Here is an example using <enter> and <leave> as #bryanoakley suggested with a toplevel (with overridedirect set to true). Use the hover_timer class for easy use of this. This needs the widget and help-text (with an optional delay argument - default 0.5s) and can be easily called just by initiating the class and then cancelling it.
import threading, time
from tkinter import *
class hover_window (Toplevel):
def __init__ (self, coords, text):
super ().__init__ ()
self.geometry ("+%d+%d" % (coords [0], coords [1]))
self.config (bg = "white")
Label (self, text = text, bg = "white", relief = "ridge", borderwidth = 3, wraplength = 400, justify = "left").grid ()
self.overrideredirect (True)
self.update ()
self.bind ("<Enter>", lambda event: self.destroy ())
class hover_timer:
def __init__ (self, widget, text, delay = 2):
self.wind, self.cancel_var, self.widget, self.text, self.active, self.delay = None, False, widget, text, False, delay
threading.Thread (target = self.start_timer).start ()
def start_timer (self):
self.active = True
time.sleep (self.delay)
if not self.cancel_var: self.wind = hover_window ((self.widget.winfo_rootx (), self.widget.winfo_rooty () + self.widget.winfo_height () + 20), self.text)
self.active = False
def delayed_stop (self):
while self.active: time.sleep (0.05)
if self.wind:
self.wind.destroy ()
self.wind = None
def cancel (self):
self.cancel_var = True
if not self.wind: threading.Thread (target = self.delayed_stop).start ()
else:
self.wind.destroy ()
self.wind = None
def start_help (event):
# Create a new help timer
global h
h = hover_timer (l, "This is some additional information.", 0.5)
def end_help (event):
# If therre is one, end the help timer
if h: h.cancel ()
if __name__ == "__main__":
# Create the tkinter window
root = Tk ()
root.title ("Hover example")
# Help class not created yet
h = None
# Padding round label
Frame (root, width = 50).grid (row = 1, column = 0)
Frame (root, height = 50).grid (row = 0, column = 1)
Frame (root, width = 50).grid (row = 1, column = 2)
Frame (root, height = 50).grid (row = 2, column = 1)
# Setup the label
l = Label (root, text = "Hover over me for information.", font = ("sans", 32))
l.grid (row = 1, column = 1)
l.bind ("<Enter>", start_help)
l.bind ("<Leave>", end_help)
# Tkinter mainloop
root.mainloop ()
I wanted to contribute to the answer of #squareRoot17 as he inspired me to shorten his code while providing the same functionality:
import tkinter as tk
class ToolTip(object):
def __init__(self, widget, text):
self.widget = widget
self.text = text
def enter(event):
self.showTooltip()
def leave(event):
self.hideTooltip()
widget.bind('<Enter>', enter)
widget.bind('<Leave>', leave)
def showTooltip(self):
self.tooltipwindow = tw = tk.Toplevel(self.widget)
tw.wm_overrideredirect(1) # window without border and no normal means of closing
tw.wm_geometry("+{}+{}".format(self.widget.winfo_rootx(), self.widget.winfo_rooty()))
label = tk.Label(tw, text = self.text, background = "#ffffe0", relief = 'solid', borderwidth = 1).pack()
def hideTooltip(self):
tw = self.tooltipwindow
tw.destroy()
self.tooltipwindow = None
This class can then be imported and used as:
import tkinter as tk
from tooltip import ToolTip
root = tk.Tk()
your_widget = tk.Button(root, text = "Hover me!")
ToolTip(widget = your_widget, text = "Hover text!")
root.mainloop()
I'm working on simple GUI but I'm stuck. This is the basic flow:
Show text text and:
save time in time_pressed
start the progressbar and update it until time_notallow expires.
If a user presses <Next> see if seconds specified in time_notallow have passed,
and if not, don't allow the display of the next text.
Basically, I want to prevent users form calling a method bind to the <Right> key until time_notallow passes, and show a progressbar to inform them how long they'll have to wait. Since I use bind, such as...
self.master.bind('<Right>', self.text_next)
...I don't have .after(), as in widgets.
What I've tried
master.after() to set bind to None and after time_notallow to bind to self.text_next, but it didn't work.
Created a thread which looped with while True to check constantly if time_notallow is passed or not, but the application crashes.
Any help appreciated.
Edit: A Solution. Using lambda() in .after to count seconds (thanks to Bryan Oakley)
"""
Stripped-down version to figue out time/event/threading stuff.
What this has to do:
1. Show the window and some text.
2. User presses Next arrow and new text shows. Paint the label red.
3. Prevent user form pressing again (unbind all keys), until 2 seconds passed
(time_wait).
4. Make notice of passed time and after 2 seconds bind the keys again and
paint the label green.
5. Loop the steps 2-4.
"""
import sys
import tkinter as tk
from tkinter import W, E, S, N
class Test(tk.Frame):
def __init__(self, master=None):
"""Draw the GUI"""
tk.Frame.__init__(self, master)
self.draw_widgets()
self.grid()
self.time_wait = 2
self.locked = False
self.bind_keys()
self.counter = 0
def draw_widgets(self):
"""Draw all the widgets on the frame."""
text = 'Just a sample sentence.'
#Label with the sentence
self.lbl_text = tk.Label(self, anchor="center", relief='groove')
self.lbl_text['text'] = text
self.lbl_text['font'] = ('Helvetica', 27)
self.lbl_text.grid(column=0, row=0, sticky=W+E+S+N)
self.lbl_note = tk.Label(self, anchor="center", relief='groove',
bg='green')
self.lbl_note.grid(column=0, row=1, sticky=W+E+S+N)
def text_next(self, event):
"""Get next text"""
if not self.locked:
self.counter += 1
self.lbl_text['text'] = 'The text number %s!' % self.counter
self.bind_tonone()
self.lock(self.time_wait)
def text_previous(self, event):
"""Get previous text"""
if not self.locked:
self.counter -= 1
self.lbl_text['text'] = 'The text number %s!' % self.counter
self.bind_tonone()
self.lock(self.time_wait)
def bind_keys(self):
"""Bind the keys"""
self.master.bind('<Left>', self.text_previous)
self.master.bind('<Right>', self.text_next)
self.master.bind('<Escape>', self.exit)
self.lbl_note['bg'] = 'green'
def bind_tonone(self):
"""Unbind the keys"""
self.master.bind('<Left>', None)
self.master.bind('<Right>', None)
self.master.bind('<Escape>', None)
self.lbl_note['bg'] = 'red'
def lock(self, n):
if n == 0:
self.locked = False
self.lbl_note['text'] = ''
self.lbl_note['bg'] = 'green'
else:
self.locked = True
self.lbl_note['text'] = 'Locked for %s more seconds' % n
self.lbl_note.after(1000, lambda n = n - 1: self.lock(n))
def exit(self, event):
"""Exit the program."""
sys.exit()
def start():
"""Start the gui part."""
root = tk.Tk()
app = Test(master=root)
app.mainloop()
if __name__ == '__main__':
start()
You don't need threads or timers to solve this problem. All you need is a procedure that takes a number of seconds to wait, and have it call itself once a second until the number gets down to zero.
It would look something like this (off the top of my head, untested):
def __init__(...):
...
self.locked = False
...
def text_next(self, event):
if not self.locked:
<do the "next" logic>
self.lock(10) # lock for 10 seconds
def text_previous(self, event):
if not self.locked:
<do the "previous" logic>
self.lock(10) # lock for 10 seconds
def lock(self, n):
if n == 0:
self.locked = False
self.status.config(text="")
else:
self.locked = True
self.status.config(text="Locked for %s more seconds" % n)
self.status.after(1000, lambda n=n-1: self.lock(n))
How about instead of a spin-waiting thread, you try the specifically-designed threading.Timer object?
A quick fix using your timer is to refactor a bit. Set your self.track initially to None, then only set it to run/block on arrow.
import sys
import threading
import Tkinter as tk
from Tkinter import W, E, S, N
class Test(tk.Frame):
def __init__(self, master=None):
"""Draw the GUI"""
tk.Frame.__init__(self, master)
self.draw_widgets()
self.grid()
# Track wait times
self.time_wait = 2
# Timer
self.track = None
self.bind_keys()
self.counter = 0
def draw_widgets(self):
"""Draw all the widgets on the frame."""
text = 'Just a sample sentence.'
#Label with the sentence
self.lbl_text = tk.Label(self, anchor="center", relief='groove')
self.lbl_text['text'] = text
self.lbl_text['font'] = ('Helvetica', 27)
self.lbl_text.grid(column=0, row=0, sticky=W+E+S+N)
self.lbl_note = tk.Label(self, anchor="center", relief='groove',
bg='green')
self.lbl_note.grid(column=0, row=1, sticky=W+E+S+N)
def text_next(self, event):
"""Get next text"""
if not self.track or not self.track.is_alive():
self.track = threading.Timer(self.time_wait, self.bind_keys)
self.counter += 1
self.lbl_text['text'] = 'The text number %s!' % self.counter
self.bind_tonone()
self.track.start()
def text_previous(self, event):
"""Get previous text"""
self.counter -= 1
self.lbl_text['text'] = 'The text number %s!' % self.counter
def bind_keys(self):
"""Bind the keys"""
self.master.bind('<Left>', self.text_previous)
self.master.bind('<Right>', self.text_next)
self.master.bind('<Escape>', self.exit)
self.lbl_note['bg'] = 'green'
def bind_tonone(self):
"""Unbind the keys"""
self.master.bind('<Left>', None)
self.master.bind('<Right>', None)
self.master.bind('<Escape>', None)
self.lbl_note['bg'] = 'red'
def exit(self, event):
"""Exit the program."""
sys.exit()
def start():
"""Start the gui part."""
root = tk.Tk()
app = Test(master=root)
app.mainloop()
if __name__ == '__main__':
start()
On preview though, #Bryan Oakley, has a better solution.