how to get a value of clicked button in different python files

134 views
Skip to first unread message

Sadaka Technology

unread,
Dec 8, 2020, 2:37:14 PM12/8/20
to Kivy users support
Hello,

if I have a main.py and Float.py:

#in main.py:

from Float import Color_

class colorscreen(Screen):
       pass      # assume I already have a screen in kv file and using function
                     #(change screen)I can open it and it will contain Color_()
class layout(FloatLayout):
       def init(..):
             super(..).init(..)
             btn = Button(on_release=self.adjust_value)
             label = Label()
             self.add_widget(btn)
             self.add_widget(label)

       def  adjust_value(self,args):
              change_screen ('color_screen')
              #if btn in Float_screen is clicked how can I get its value here in this 
              #function ?

#Float.py:

class Float_screen(GridLayout):
         def init(..):
             super(..).init(..)
             self. data = {'1st_text','2nd_text'}

           for k in self.data:
                    btn = Button(on_release:partial(self.get_data,k))
                    self.add_widget(btn)
        def get_data(self,k,args):
                  self.color_value = k

Sadaka Technology

unread,
Dec 8, 2020, 2:38:50 PM12/8/20
to Kivy users support
#if btn in Float_screen is clicked how can I get its value here in this function ?   how can I get the value k

Sadaka Technology

unread,
Dec 8, 2020, 2:54:00 PM12/8/20
to Kivy users support
In details, I want to get a color code from another class in another .py file than main file, I have this .py file I updated (Color_Change_Screen.py):

#in main.py:

from Color_Change_Screen import  Color_change_layout  

#I have a screen which will lead eventually to a kv file ex:

class color_screen(Screen)

#in kv file I have :
     <color_screen>:
              Color_change_layout:         
# and thats it:

# I made a function called it (change_screen) which will change in between screens:

#now suppose in main.py:

class Layout(FloatLayout):
       def init(..):
             super(..).init(..)
             btn = Button(on_release=self.adjust_value)
             label = Label()
             self.add_widget(btn)
             self.add_widget(label)

       def  adjust_value(self,args):
              self.change_screen ('color_screen')
              
# and in (App) build function I am returning Layout()

# in this function (adjust_value) once I click on the button it will change the screen to color_screen ,   their I have many buttons, if I click any it will return whatever in change_color function in .py I sent, in that function each button will return different value and key, how can I get that value aand key in (adjust_value) function once the button is clicked, also how can I change screen back to main_screen, note that I need this method for many kind of buttons not only this button in main.py Layout() class, so I am trying to avoid making lots of .py files , and make it only one .py file for changing the colors, so I want one common method for all!
Color_Change_Screen.py

Elliot Garbus

unread,
Dec 8, 2020, 9:16:30 PM12/8/20
to kivy-...@googlegroups.com

I can’t figure out your code example.  I’ll take a few guesses and make some suggestions.  Please follow up if I don’t understand.

 

It is difficult for me to see where you have instanced the class.

In kv when I define a rule

 

<ColorScreen>:   # this defines a rule in kv, this is equivalent to declaring a class in Python.

    ColorChangeLayout:  # This is an instance of the ColorChangeLayout class.  It is a member of the ColorScreen.  It will be instanced when the ColorScreen is instanced.

        id: color_change_layout_id   # I can give this an id

 

In kv I could instance the ColorScreen under a screenmanger.

 

BoxLayout:              # The root widget

    ScreenManger:

        id: sm

        ColorScreen:

            name: ‘color_screen_1’

        ColorScreen:

            name: ‘color_screen_2’

        OtherScreen:

            name: ‘another_screen’

 

 

The root widget and each kivy rule have their own ids dictionary.

To access the ColorChangeLayout class that is on color_screen_1 from python I would do the following:

 

class SomeClass(BoxLayout):

def method_to_access_color_change_layout(self):

    app = App.get_running_app()

    app.root.ids.sm.get_screen(‘color_screen_1).ids.color_change_layout_id.method_of_color_screen()

 

Breaking this down:

app – is the App class

app.root – is the root widget, a BoxLayout in this example

app.root.ids  - the ids dictionary of the root.

app.root.ids.sm –this is the ScreenManager

app.root.ids.sm.get_screen(‘color_screen_1)    - This is the instance of the ColorScreen with the name color_screen_1

app.root.ids.sm.get_screen(‘color_screen_1).ids  - the ids dict of ColorScreen

app.root.ids.sm.get_screen(‘color_screen_1).ids.color_change_layout_id  - This is the instance of ColorChangeLayout on the ColorScreen

app.root.ids.sm.get_screen(‘color_screen_1).ids.color_change_layout_id.method_of_color_screen()  # this is how you would access a method of the ColorScreen for this instance.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/191efd03-88b6-4dbf-bdda-f7658957aefbn%40googlegroups.com.

 

Sadaka Technology

unread,
Dec 9, 2020, 2:09:07 AM12/9/20
to Kivy users support
look, I have 2 python files, one is main and another is color, I have many functions in main.py that requires opening color.py where I will get a screen that will have different colored ellipse widgets and a label bellow each which text is equal to the color type of each, both of ellipse widgets and labels are having a button behavior, so I can click on them, the thing is , I dont want to make color.py for each function I am applying in main.py because each function in main.py will change a color of a certain widget  and these functions are called when clicking of buttons, 

so suppose, the user clicks on any of these buttons, I want it to change the screen to color_screen (already I have done) which will include the class in color.py which will show the color widgets, the question is how to return widget color that the user has clicked, after clicking on any of the ellipse button widgets, I want that widget color in main.py to have same color of that widget in color.py class once he clicks on it, I can do it individually for each function, but that means I have to make several color.py files in different names, which I am trying to avoid

Sadaka Technology

unread,
Dec 9, 2020, 2:38:16 AM12/9/20
to Kivy users support
nevermind I got it , 

thanks for the big support man! 

just a question, what is the funcionality of fbind, what is the difference between fbind and bind ?

Elliot Garbus

unread,
Dec 9, 2020, 9:00:34 AM12/9/20
to kivy-...@googlegroups.com

I use a lot of kv, I rarely use the bind method and have not had a need to use fbind directly.

I have internalized that it is faster with limited error checking.

Here are the docs: https://kivy.org/doc/stable/api-kivy.event.html?highlight=fbind#kivy.event.EventDispatcher.fbind

Message has been deleted

Sadaka Technology

unread,
Dec 9, 2020, 3:16:46 PM12/9/20
to Kivy users support
same thing for me , so that means no need to learn it hahahah,

I am have an issue with MDTextfield, its pos_hint_y always changing when parent height changes, how can I make it fixable at a certain level! 

can I use pos_hint_x = something , then do .y = something ?  how can I only adjust value of pos_hint_x only ?! 

On Thursday, December 10, 2020 at 12:15:42 AM UTC+4 Sadaka Technology wrote:
same thing for me , so that means no need to learn it hahahah,

I am have an issue with MDTextfield, its pos_hint_y always changing when parent height changes, how can I make it fixable at a certain level!

Elliot Garbus

unread,
Dec 9, 2020, 4:49:40 PM12/9/20
to kivy-...@googlegroups.com

I don’t know about the specifics of MDTextfield, but in general to fix a size of a widget you must set the size_hint to None.

 

To fix  size:

size_hint: None, None

 

To fix height:

size_hint_y: None

height: 200

 

To fix width:

size_hint_x: None

width: 200

Sadaka Technology

unread,
Dec 10, 2020, 9:49:44 AM12/10/20
to Kivy users support
when ever I am chaging font_size or height of parent , mdtext_field position is changing, while the rest of widgets are not , all widgets are specified with size_hint(None,None) and specific size values , for MDTextField I have size_hint_y = None and height I adjusted to 25, why am I getting different positions

Elliot Garbus

unread,
Dec 10, 2020, 10:18:37 AM12/10/20
to kivy-...@googlegroups.com

Take a look at the source:

https://github.com/kivymd/KivyMD/blob/master/kivymd/uix/textfield.py

 

What layout are you using?

You are seeing the position change.  How are you specifying the position?

The size of the graphics in MDTextField are sized dynamically.

 

Use the inspector to get a better idea of what is going on…

 

python mymain.py -m inspector

 

Then press control-e, and select MDTextField, and examine the properties.

Reply all
Reply to author
Forward
0 new messages