Answer the question
In order to leave comments, you need to log in
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()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question