How to access which stack item was pressed with kivymd FloatingActionButtonSpeedDial? - python

I want to use KivyMDs FloatingActionButtonSpeedDial, with something different happening depending on which stack button is pressed. Right now, I'm just trying to print which button was pressed.
I've found outdated information from tutorials, but it looks like the current way to set a function on the button press is:
on_press_stack_button: root.callback(self)
then in my .py file I have
def callback(self, instance):
print(instance.icon)
but no matter which stack icon I choose, the result that's printed is always 'plus' (the root icon)
I'm guessing this is because I'm passing self into the callback function, but what else am I supposed to pass to access the actual button that's clicked? I can't find any examples in the docs, and all the tutorials I find say to use
'''
callback: callback
'''
instead of on_press_stack_button:
but using callback doesn't print anything at all.
Any help is greatly appreciated!

Related

Using Python's tk_tools.py led

I need some help with the led option 'on_click_callback=' of the led widget in the module tk_tools.py. Here's what I have:
reactor_led = Led(win, size=50, toggle_on_click=True, on_click_callback=led_clicked(ON))
reactor_led.to_red()
reactor_led.to_green(on=True)
reactor_led.grid()
The led does get displayed properly in its window, and it does react correctly to a mouse click. I've omitted the code for led_clicked, but it is being called when I click the 'led'. The parameter "ON" is the boolean state of the led when clicked, but I can't come up with the actual code to reference that to pass to the call-back function. I need a working example to understand it.
Thanks

text editor - make bold button sunken

I'm working on a bold button for a text editor. I'm having trouble with setting the button "sunken" when bold text is selected.
I imagine it will be something like the below?
self.textPad.tag_bind("bt","<Button-1>",self.boldP())
def boldP(self):
self.boldB.config(relief="sunken")
Not sure if I got the right idea, could someone please shed some light on this for me?
I'm not sure it's the main problem, but one problem with the code snippet that you posted is that you are not actually giving a callback to tag_bind, you are giving it None. Why? Because you are calling boldP, which returns None. Change this line:
self.textPad.tag_bind("bt","<Button-1>",self.boldP())
To:
self.textPad.tag_bind("bt", "<Button-1>", self.boldP)
Another problem is that your callback should take an event argument:
def boldP(self, event):
...
I'm not too familiar with Tkinter, so I'm not so sure about how tag_bind is supposed to work - but it seems like it's mostly used with canvas objects. Is this what you are doing? Otherwise try bind. Something like this:
self.button.bind(,"<Button-1>", self.boldP)

Give/get arguments in popup from "on_press"

Pardon me for my simple question, but I don't understand some thing.
I want to give a few arguments from button which is located in popup window in one method to another method.
Example:
.py code
class GeneralForm(TabbedPanel):
def EDIT(self,D):
box1=BoxLayout(orientation='vertical')
t1=TextInput(text=GeneralForm.PARSE(self,D))
b2=Button(text='Save')
b3=Button(text='Cancel')
box2=BoxLayout()
box2.add_widget(b2)
box2.add_widget(b3)
box1.add_widget(t1)
box1.add_widget(box2)
popup = Popup(content=box1,auto_dismiss=False,size_hint=(.75,.75),title='Edit')
b2.bind(on_press=self.SAVE_EDIT) <====== There is a problem
b3.bind(on_press=popup.dismiss)
popup.open()
def SAVE_EDIT(self,instance):
!!! DOING SOMETHING !!!
https://s3.amazonaws.com/xasan/snapshot/stack1.png
What I want:
In method "EDIT" I have text input "t1". After changing text in this text input I press button "b2" which calls method SAVE_EDIT with two arguments.
So, I want to give the third agrument to "SAVE_EDIT" method which will return an edited text in t1.
Something like this:
.py code
class GeneralForm(TabbedPanel):
def EDIT(self,D):
box1=BoxLayout(orientation='vertical')
t1=TextInput(text=GeneralForm.PARSE(self,D))
b2=Button(text='Save')
b3=Button(text='Cancel')
box2=BoxLayout()
box2.add_widget(b2)
box2.add_widget(b3)
box1.add_widget(t1)
box1.add_widget(box2)
popup = Popup(content=box1,auto_dismiss=False,size_hint=(.75,.75),title='Edit')
b2.bind(on_press=self.SAVE_EDIT(t1.txt)) <====== There is a problem
b3.bind(on_press=popup.dismiss)
popup.open()
def SAVE_EDIT(self,instance,TEXT): <====== There is a problem
!!! DOING SOMETHING with TEXT!!!
Step-by-step:
Popen window was opened with some text in the text input.
We edited text, deleted something or added.
We are clicking on button "Save"(b2) and all text in txt input(t1) push to method "SAVE_EDIT" where we save,parse or do something else
with this text.
Thanks in advance.
You could use lambda:
on_press=lambda instance, text=t1.txt: self.SAVE_EDIT(instance, TEXT=text)
Or functools.partial():
on_press=partial(self.SAVE_EDIT, TEXT=t1.txt)
Both variants use t1.txt at the time of the bind call i.e., the value may be obsolete by the time you press the button.
To use the current most recent value:
on_press=lambda instance: self.SAVE_EDIT(instance, TEXT=t1.txt)
In this case, t1.txt is called each time the callback is invoked.

Is there a way to press a button without touching it on tkinter / python?

Hi I need to do this because, I am making a matching / memmory game, and there has to be a button (Totally separated from the ones on the current game) that when I press it, it has to show the matching cards automatically without having to touch the buttons with the mouse.
Is there a "press" function or something like that for pressing the button?
Thanks! :)
As Joel Cornett suggests in a comment, it might make more sense to simply call the callback that you passed to the button. However, as described in the docs, the Button.invoke() method will have the same effect as pressing the button (and will return the result of the callback), with the slight advantage that it will have no effect if the button is currently disabled or has no callback.
If you also want visual feedback for the button you can do something like this:
from time import sleep
# somewhere the button is defined to do something when clicked
self.button_save = tk.Button(text="Save", command = self.doSomething)
# somewhere else
self.button_save.bind("<Return>", self.invoke_button)
def invoke_button(self, event):
event.widget.config(relief = "sunken")
self.root.update_idletasks()
event.widget.invoke()
sleep(0.1)
event.widget.config(relief = "raised")
In this example when the button has focus and Enter/Return is pressed on the keyboard, the button appears to be pressed, does the same thing as when clicked (mouse/touch) and then appears unpressed again.

Handling events from file menu

I am trying to code a program with a regular file menu. (e.g. File, Edit, View, etc).
I want the action they take in that menu to update my status bar (a label).
The problem is, the way I have it setup now, I believe it's executing the command and then trying to take the result as what it should do.
Currently a menu item is defined like so:
fileMenu.add_command(label="Insert", command=self.statusUpdater(statusLabel,"Insert Triggered")
And the function statusUpdater is defined as such:
def statusUpdater(self,status,commandName):
status.config(text=commandName)
status.update_idletasks()
So the problem is, right at the start of the program, the status changes to "Insert Triggered". What I want is for that to only happy once I have actually clicked "Insert"
From hints I've seen elsewhere it seems like I need some way to pass and handle the event of Insert being clicked.
Could someone supply a generic and basic function that does what I ask? I think the problem lies in the () attached to the command function, but I don't know any other way to pass arguments.
All i need is a function that is called on the click event, and knows which fileMenu command triggered it.
Thanks!
Commands take a reference to a function. You can se a lambda if you want to pass it arguments:
...command=lambda l=statusLabel, self.statusUpdater(l, "Insert Triggered"))

Categories