Does customtkinter CTkButton hover has event option? - python

I want to perform an action when a mouse hover event occurred on customtkinter ctkbutton hover. is it yet implemented?

Per the CTkButton source code, the on_enter method is bound to the <Enter> event. This method is predominantly focused on updating the button's appearance on hover. If you want to trigger an additional callback on hover, you'll have to add another binding to the button
def callback(event):
# put whatever you want to do 'on hover' into this function
print('Button hovered!')
button = CTkButton(parent, text='Button!')
# use '+' to avoid overwriting the existing binding
button.bind('<Enter>', callback, add='+')
button.pack()

Related

Python TKinter : Manually Trigger a Bind Event (Specifically Configure)

I'm busy working with a TKinter GUI and am trying to append additional Items into a Scrollbar Canvas. The scrollbar bits seem to be working just fine
def canvas_configure(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
canvas.bind("<Configure>", lambda event: self.canvas_configure(event))
The configure function triggers on resizes just fine but does not trigger when appending Items, I was wondering if it's possible to manually trigger the <Configure> (or other events for that matter)
def append_to_canvas(self, parent):
label = tk.Label(parent, text="Yes")
label.pack();
# canvas execute <Configure>
NOTE
The way I handle situations like this at the moment is to just execute the same function that the event would be executing, for example
def append_to_canvas(self, parent):
label = tk.Label(parent, text="Yes")
label.pack()
event = # create imposter event here
self.canvas_configure(event)
But this isn't always practical
CLARITY
The example I'm using above is just an example. I'm more concerned with the Manual Triggering of a Bind Event part of the question.
This applies to other bind events such as
button.bind("<Button-1>", lambda event: exec_button_click());
I think I've found out how to do this, it looks like there's a function for this feature called event_generate
self.canvas.event_generate("<Configure>");

tkinter check if Shift is down when Button is pressed

I have a tk.Button(..., command=_on_button_click).
Once it is clicked, I want to know whether the Shift key is currently held down.
But the _on_button_click is called without any event object or similar for me to check it
Assuming a button named button and a handler named shift_click, you can use the bind method:
button.bind("<Shift-Button-1>", shift_click)
Of course, you will probably also want one without shift click:
button.bind("<Button-1>", not_shift_click)

How to run a method on keypress in tkinter

I have an entry field, and as I type into the data field, I want a method, that updates a treeview widget to be ran. Currently, I can type in a search parameter, then press a 'search' button and it will run the method to search through the treeview to find specified clients, but I want to the treeview to be updating whilst typing into the entry, not by a button press
I am unsure as to weather this is possible, if it should be doing by binding keys or if there is a way using the event loop to achieve this?
See this SO post:
TkInter keypress, keyrelease events
Essentially:
from Tkinter import *
def keyup(e):
pass;
# e.char contains the pressed key if you need that info
# use your search function here
Edit (Sorry I forgot this):
You'll need to bind the keyup function to your widget with something like:
frame.bind("<KeyRelease>", keyup) # you can also bind to a search widget

What is the difference between command and bind in tkinter?

I'm trying to make a button print a string when it's pressed and print another when it's released. I know about the command atribute and the bind method, but I would like to know if it's possible to accomplish it only using atributes or if I have to use methods. With this piece of code:
class motor:
def __init__(eleMesmo, eixo , valorZero):
eleMesmo.eixo = eixo
eleMesmo.zero = valorZero
def aumenta(self):
print(self.eixo + str(self.zero+5))
def diminui(self):
print(self.eixo + str(self.zero-5))
def para(self):
print(self.eixo + str(self.zero))
eixox = motor('x',90)
eixoy = motor('y',90)
class Interface:
def __init__(elemesmo, widget):
quadro = Frame(widget)
quadro.pack()
elemesmo.aumentarY = Button(quadro,text="Aumentar Y",height=10,width=20,command=eixoy.aumenta)
elemesmo.aumentarY.pack(side=TOP)
elemesmo.diminuirY = Button(quadro,text="Diminuir Y",height=10,width=20,command=eixoy.diminui)
I can call the method aumenta for object eixo y when button aumentarY is pressed. I would like to call the method para for object eixo y when button aumentarY is released. How I can do it?
All Event types are outlined here, you are looking for <Button-1> (click down on button 1 (left mouse button if you are right handed)) and <ButtonRelease-1> (release mouse button 1 (left button if you are right handed)).
Note I wouldn't use command if you bind both of these.
elemesmo.aumentarY = Button(quadro,text="Aumentar Y",height=10,width=20)
elemesmo.aumentarY.bind("<Button-1>",eixoy.aumenta)
elemesmo.aumentarY.bind("<ButtonRelease-1>",eixoy.para)
However you must know that when using bind the callback is called with an Event object, if you don't need it you can just add an optional and unused parameter to the callback:
def aumenta(self, event=None):
print(self.eixo + str(self.zero+5))
def diminui(self, event=None):
print(self.eixo + str(self.zero-5))
def para(self, event=None):
print(self.eixo + str(self.zero))
Both command and .bind method are used to add life and functionality to a button, using the tkinter module on Python.
Command Parameter:
tkinter.Button(frame, text="Exit", fg="green", bg="pink", command=master.destroy)
The above button would destroy the frame we the 'Exit' button is selected or clicked any how. Try using the tab key and pressing spacebar. You will notice that the button would work.
What if you don't want that?
What if you strictly want the user to click it using the left mouse button?
Note that, you can pass a simple zero-parameter method to the command parameter, which may or may not contain any event object.
Bind Method:
The bind method is used to add extra information/functionality to the button, the way it needs to be clicked, the specific button that needs to be used and so on. It looks something like this:
btn = Button(frame, text="Exit", fg="green", bg="pink")
btn.bind(sequence="<Button-1>", func=master.destroy)
This will work only when the Mouse Button-1 (Left-Mouse Button) is pressed. Some alternate versions to this are like:
btn.bind(sequence="<ButtonRelease-1>", func=master.destroy)
The above works when the Mouse Button-1 is pressed and released.
btn.bind(sequence="<Double-Button-1>", func=master.destroy)
Similarly, the above sequence works only when the Mouse Button-1 is Double Clicked.
Note that, in the bind method you cannot pass a simple zero-parameter method to the bind method, it must contain one event object as shown below.
def callback(event):
pass
For the entire list of sequences for bind method, refer this article.

Tkinter binding mouse double click

I'm trying to bind my mouse double click to a function which for now just prints the current selection in a Tkinter list box. To be clear the function should only print when the user double clicks on one of the items of a Tkinter list box. What event binding should I use?
You can bind to <Double-Button-1>:
widget.bind('<Double-Button-1>', handler)
There is also <Button-1> for normal mouse clicks and <Triple-Button-1> for a triple mouse click.
For more information on bindings in Tkinter, see Events and Bindings.
You have to realize that there is a hierarchy to all widgets, and this means that for each widget you click, multiple bindings are possible. If you don't override the default action, each hierarchy's default handler gets called, starting at the lowest level (such as your Listbox) and going all the way up to the Tk() or Toplevel() widget. For you, since you want to print only when a listbox item is clicked, you can bind to the listbox widget, as follows:
listboxWidget.bind('<Double-Button-1>', listboxWidget_leftclick_handler)
Then, when you enter the def listboxWidget_leftclick_handler(event) function, you don't have to check the event.widget value to see if it's the name of your Listbox widget. But you could also check at a higher level (bind a handler to a higher-level widget) and check event.widget to see which widget was clicked.
Also note that the only way to prevent the entire hierarchy of event handlers from triggering is by using a return 'break' from your custom handler, but you usually only need to do this if later handlers corrupt what your custom handler has done.
Additional info about default handlers
The other part which I left out is that there is also a "default" handler for most events. If you bind your own handler, once it's finished, if you don't return 'break', the default handler will be called next.
For example, say you want to make your own Entry box into a password entry. By default, when you type alphanumeric chars when the Entry has focus (which means it's getting input from the keyboard), the chars will appear in the Entry. You can bind:
myEntry.bind('<KeyPress>', passworder)
where passworder is your custom handler which grabs the event holding your inputted char and then outputs an asterisk into the Entry instead. But, if you don't use a return "break" at the end of your handler, the Entry widget is still going to see that char that you didn't want shown, because once your handler is done inserting the asterisk, the default handler will simply insert the typed char (like it would normally). But, if you do the return 'break', the default handler won't get called, and the typed char(s) won't appear in the Entry.
As an add-on. In order to distinguish action between a single click and a double click, delay the call to mouse action for a brief period to allow for the double click flag to be set. See below example:
from tkinter import *
def mouse_click(event):
''' delay mouse action to allow for double click to occur
'''
aw.after(300, mouse_action, event)
def double_click(event):
''' set the double click status flag
'''
global double_click_flag
double_click_flag = True
def mouse_action(event):
global double_click_flag
if double_click_flag:
print('double mouse click event')
double_click_flag = False
else:
print('single mouse click event')
root = Tk()
aw = Canvas(root, width=200, height=100, bg='grey')
aw.place(x=0, y=0)
double_click_flag = False
aw.bind('<Button-1>', mouse_click) # bind left mouse click
aw.bind('<Double-1>', double_click) # bind double left clicks
aw.mainloop()

Categories

Resources