Give/get arguments in popup from "on_press" - python

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.

Related

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

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!

Generate buttons in Tkinter with a different name each time

I want to generate a button for each .py document in a file, and each button give a different argument to the function
for name in files:
if name[-3:]=='.py':
a=name[:-3]
button=Button(barre, text=a, command=python_file(a))
button.pack()
def python_file(name):
text = Text(barre)
text.insert(name)
os.popen("python3 "+a+".py","r")
It basicly write the name of the python file and execute. But, I need to give a different name to each button, just like : button1, button2 (...).
Also when I run my code, It execute the function each time thee is a new python file.
Thx for helping !!
Change the command argument in your button to:
button=Button(barre, text=a, command=lambda a=a: python_file(a))
The argument passing to your function is the one which is the last value in the for loop. To pass the current value, you need to specify it through the lambda function.
Also, change the os.popen method like this to fully execute the code inside the file.
c = os.popen("python3 "+a+".py","r")
print(c.read())

Blocking The Tab Key

I've recently made a text editor with tkinter for python.
I need a way to disable tab from being able to be used normally, so it doesn't indent.
Does anyone have any idea as to how I would achieve this?
Thank you for your time.
It really depends on exactly what you did. Without more information I'm going to assume that you have a text widget somewhere and that you want to disable tab from indenting there.
Example:
from tkinter import Tk, Text
def no_tab(event):
return 'break'
root = Tk()
text_widget = Text()
text_widget.pack()
text_widget.bind('<Tab>', no_tab)
root.mainloop()
In this example we bind the <Tab> key to the function no_tab. So everytime tab is pressed within the text widget the no_tab function is called. The no_tab function returns the magic string 'break' which means that the action of the key won't be preformed and thus disabling the indentation that the tab key would have created otherwise.

Python: button command isn't running?

For an A-level computing project, I am making a car data monitoring system. I have a button that opens the filedialog.askopenfilename method. When I pass this through a method like below, it doesn't work. However when I pass it straight into the button, it works fine. Any ideas as to why?
Doesn't work:
def get_data_file():
filedialog.askopenfilename
return
OpenfileButton=Button(master,text="Select File",width=20,command=get_data_file).grid(row=3, column=2)
works:
OpenfileButton=Button(master,text="Select File",width=20,command=filedialog.askopenfilename).grid(row=3, column=2)
You need to actually call the function
def get_data_file():
filedialog.askopenfilename()
When you pass the function to the button you should not call it but simply pass it to be called when the button is clicked, but as you have now wrapped it in another function it must be called by you.
The return is redundant and can be left out if you wish. All python functions return None by default.

More information in signal/slot with QPushButton

When I implements the function that is executed when a button is clicked, the code is like this:
self.connect(btnBrowse, SIGNAL("clicked()"), self.browseFile)
and I implement the function browseFile
def browseFile(self):
But when i am inside the method browseFile, I don't have information about the button being clicked, because I want to implement just one function browseFile for many buttons. how can I do to have more information the slot, for example to have my function looks like this:
def browseFile(self, option):
Thanks
Connect to a lambda (or use functools.partial).
Also note the use of new style syntax, which is much more readable and pythonic.
self.btnBrowse.clicked.connect(lambda: self.browseFile(option))
Sender() provides a pointer to the button that sent the event, you can then read the button text (or other associated data) to determine which button was pressed

Categories