O
O
OCETP112021-06-08 19:29:42
Kivy
OCETP11, 2021-06-08 19:29:42

Why does a reference error occur in Python?

Kivy module. Well described here -

In general, it complains about line 30 (textInput.text = button_text).

Full error text: File "F:\python\myCacl.py", line 30, in on_button_press
textInput.text = button_text
NameError: name 'textInput' is not defined

Code:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput

buttons = [
    ["7", "8", "9", "/"],
    ["4", "5", "6", "*"],
    ["1", "2", "3", "-"],
    [".", "0", "C", "+"],
    ]
class MainApp(App):
    def build(self):
        layout = BoxLayout(orientation="vertical")
        textInput = TextInput(multiline=False, readonly=False, halign="right", font_size=55)
        layout.add_widget(textInput)
        textInput.text = "0"
        
        for i in buttons:
            help_layout = BoxLayout()
            for label in i:
                button = Button(text=label,pos_hint={"center_x": 0.5, "center_y": 0.5})
                button.bind(on_press=self.on_button_press)
                help_layout.add_widget(button)
            layout.add_widget(help_layout)
        return layout

    def on_button_press(self, instance):
        button_text = instance.text
        textInput.text = button_text
            
if __name__ == "__main__":
    app = MainApp()
    app.run()

PS To run the script, you need to install the module by typing pip install kivy in cmd

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kakaduwka, 2021-06-08
@OCETP11

Try to change
self. textInput = TextInput(multiline=False, readonly=False, halign="right", font_size=55)
self. textInput.text = button_text

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question