Everytime i press the plus_btn num should be added by 1 but it only adds it a single time
i tried using a global varaible and placing the function outside of the class but it didnt work
import bidi.algorithm
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class RootWidget(App):
def build(self):
inside = FloatLayout()
label1 = Label(text='hi', font_name='fnnt',pos_hint ={'x':.15, 'y':.15})
def plus(instance):
num = 0
if label1.text == 'hi' and num <= 7:
num = 1
print(num)
plus_btn = Button(text=' ', on_press=plus)
inside.add_widget(plus_btn)
inside.add_widget(label1)
return inside
if __name__ == '__main__':
RootWidget().run()
CodePudding user response:
The num
variable in your plus()
method is a local variable for that method and you are resetting it to 0
every time that method is executed. You can correct that in many ways, but a simple way is to just declare a num
variable in your RootWidget
class, like this:
class RootWidget(App):
def build(self):
self.num = 0
inside = FloatLayout()
label1 = Label(text='hi',pos_hint ={'x':.15, 'y':.15})
def plus(instance):
# num = 0
if label1.text == 'hi' and self.num <= 7:
self.num = 1
print(self.num)
plus_btn = Button(text=' ', on_press=plus)
inside.add_widget(plus_btn)
inside.add_widget(label1)
return inside