how to set label text by accessing key or value from dict

ashikuzzaman

i want to acces key from dict. here's a textinput. i gave that textinput an id:word. if any text from that textinput matches any of these key or value from that dict, then the text of a label should be changed to show some text. i can access key or value from dict.

but problem is when i use if self.ids.word in Dict.keys(): then show something. but self.ids.word is a string data and any key or value from dict is Nonetype class. so self.ids.word cant be in dict. cause str cant be in dict(while dict key or value ls nonetype)

how can i do that?

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.properties import *

Dict={"my": "amar","i":"ami","you":"tumi"}


class pop(Popup):
    add=StringProperty()
    a=app.root.yo.ids.word.text
    if a in Dict.keys():
        self.add=str(a) + str(Dict.get(a))
class yo(BoxLayout):
    def pop(self):
        po=pop()
        po.open()
class go(BoxLayout):
    main=ObjectProperty(None)
    def yo(self):
        self.clear_widgets()
        self.main=yo()
        self.add_widget(self.main)

Builder.load_string('''
<go>:
    Button:
        text:"go"
        on_press:root.yo()
<yo>:
    TextInput:
        id:word
    Button:
        text:"press"
        on_press:root.pop()
<pop>:
    title:"pop"
    size_hint:0.8,0.3
    Label:
        text:root.add

''')

ikolim

Question 2

i want to get these key and ids in a popup label. i know in kivy i should use a=app.root.yo.ids.word.text. but how to access that id in py code?

Solution

  1. Implement a constructor for class pop().
  2. Use App.get_running_app() to get an instance of your app
  3. use root.main to get an instance of your yo object

Snippets - py file

class pop(Popup):
    add = StringProperty()

    def __init__(self, **kwargs):
        super(pop, self).__init__(**kwargs)
        a = App.get_running_app().root.main.ids.word.text
        if a in Dict.keys():
            self.add = str(a) + str(Dict.get(a))

Question 1

but problem is when i use if self.ids.word in Dict.keys(): then show something. but self.ids.word is a string data and any key or value from dict is Nonetype class. so self.ids.word cant be in dict. cause str cant be in dict(while dict key or value ls nonetype)

Solution

self.ids.word is a reference to the TextInput object. Therefore, you want to access the text attribute of the TextInput object.

Replace a = self.ids.word with a = self.ids.word.text

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to sum value from list of dict and set it to new key

Accessing Text Label Value Javascript

How to set a dict value using another key of the same dict

How to set text to a label on keyboard key press in JavaFX application?

Search text in column A for dict key, if found then set column B to dict value in Pandas dataframe

Accessing value from key - pyspark

Set the value of textbox in Bootstrap Modal from label text

Accessing value in a dict ignoring the key's second value

How to append key value pair to an existing list of dict from another list of dict based on matching Key in Python

Python: how to remove a value from a dict if it exactly matches the key?

How to read from a file into a dict with string key and tuple value?

How to use key:value pairs from a dict to build a pandas filter

How to set and get value from the composite key

pandas dataframe from_dict - Set column name for key, and column name for key-value

get key - value from dict and remove this key

How to do the key and value output from the dictionary in the label?

How to extract key and value from a text file

How to set the .Text property of a label from a "string" + stringVariable

How to set a Label.Text from a specific Column of an Access DB

Set ET.SubElement.text to dict.value if dict.key equals other XML node within same parent

How to set dict index key before looping through data to get key,value?

Create dict from list of strings by spiting text from number and using it for key: value pairs

Xamarin forms: How to change the label text value from viewmodel?

How can I find a specific key from a python dict and then get a value from that key in Python

Accessing a key's value from json body

how to return value from key/value dict using SQL from a table that stores keys?

Ansible - set fact for key of dict based on sub-key value

Return key associated with min value of a dict if min key is not in set

How to grep for value in a key-value store from plain text