0

I have a label which when you click on it prints its value. It should also change the label on the first screen. Changing that label works as it should, but when I use the same method from outside the main class it does not work. Because of that I think the problem haws something to do with making new instances of classes instead of updating the ones that are already there. My knowledge of OOP is not good and because of that I can't figure this out. Any help and/or tips would be appreciated.

I tried resolving the problem with solutions from : Instantiate a class but don't call its __init__ method , but could not make it work in my case

The problem starts at line 93/94, then goes to line 45 from then it's clear

from kivy.app import *
from kivy.uix.button import *
from kivy.graphics import *
from kivy.uix.widget import *
from kivy.uix.label import *
from kivy.uix.behaviors import *
from kivy.uix.floatlayout import *
from kivy.uix.boxlayout import *
from kivy.uix.scrollview import ScrollView
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
brojPjesmi = 50

def dobijJenegaArtista(kega):
    f = "Hi"+ str(x[0])
    return f
x = [0]
#---------------------------------------------------------------------------------
class Name(Widget):
    def __init__(self, **kwargs):
        super(Name, self).__init__(**kwargs)
    def changeName(self):
        self.nam = "yeah"
        self.ids.nameOfSong.text = self.nam[0]
        self.ids.artistOfSong.text = str(dobijJenegaArtista(x[0]))
#---------------------------------------------------------------------------------
class FirstWindow(Screen):
    def __init__(self, **kwargs):
        super(FirstWindow, self).__init__(**kwargs)
        self.pos = (0, 0)
        self.size = (1,1)
        self.z = Name()
        self.add_widget(self.z)

    def next(self):
        if x[0] == brojPjesmi:
            x[0] = 0
        else:
            x[0] = x[0] + 1
        self.z.changeName()

    def selected(self,p):
        x[0] = p - 1      # it puts the value clicked to my counter x, because 
        # I will send it to next which will add 1 i subtract 1
        print(x[0])   # check to see if it is correct
        self.next()   

    def last(self):
        if x[0] == 0:
            x[0] = brojPjesmi
        else:
            x[0] = x[0] - 1
        self.z.changeName()


#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------
class SecondWindow(Screen,BoxLayout):
    def __init__(self, **kwargs):
        super(SecondWindow, self).__init__(**kwargs)
        self.d = MyRow()
        self.add_widget(self.d)
#---------------------------------------------------------------------------------
g = []
class MyRow(ScrollView):
    def __init__(self, **kwargs):
        super(MyRow, self).__init__(**kwargs)
        self.size_hint=(1,None)
        self.size=(Window.width,Window.height)
        layout = GridLayout(cols = 1,spacing= 10,size_hint_y = None)
        layout.bind(minimum_height = layout.setter('height'))
        self.butt = Button(text="gmm",height=40,size_hint_y = None,on_press = lambda x:self.change_screen())
        layout.add_widget(self.butt)
        for i in range(brojPjesmi):
            self.z = OneRow()
            u = self.z.make()
            layout.add_widget(u)
        self.add_widget(layout)
    def change_screen(self):
        App.get_running_app().root.transition.direction = "right"
        App.get_running_app().root.current = 'main'

i = [0]
#---------------------------------------------------------------------------------
class OneRow(ButtonBehavior,GridLayout):
    def mm(self):
        print(self.i)
        self.z = MyRow
        self.z.change_screen(self)
        self.man = FirstWindow()
        self.man.selected(self.i)
    def make(self):
        i[0] = i[0] + 1
        self.i = i[0]
        self.cols = 3
        self.rows = 1
        self.height = 25
        self.size_hint_y = None
        self.on_release = self.mm
        self.Treci = Label(text = str(""),size_hint_y= None,size_hint_x =.1,height=25)
        self.prviDio = Label(text = str(self.i),size_hint_y= None,height=25)
        self.drugiDio = Label(text = str(self.i*2),size_hint_y= None,height=25)
        self.add_widget(self.Treci)
        self.add_widget(self.prviDio)
        self.add_widget(self.drugiDio)
        return self
#---------------------------------------------------------------------------------

class langApp(App):
    def build(self):
        return kv
#---------------------------------------------------------------------------------

class WindowManager(ScreenManager):
    pass
kv = Builder.load_file("nu.kv")
if __name__ == '__main__':
    langApp().run()

.kv file

#:kivy 1.11.1
WindowManager:
    FirstWindow:
    SecondWindow:
<FirstWindow>:
    name: "main"
    FloatLayout:
        pos: 0,0
        size: root.width,root.height
        Button:
            on_press: root.last()
            text: 'Last'
            pos_hint: {'x':.25,'y':.1}
            size_hint: .1,.1
        Button:
            on_release: 
            text: 'Stop'
            pos_hint: {'x':.45,'y':.1}
            size_hint: .1,.1
        Button:
            on_press: root.next()
            text: 'Next'
            pos_hint: {'x':.65,'y':.1}
            size_hint: .1,.1
        Button:
            on_release:
                app.root.current = "second"
                root.manager.transition.direction = "left"
            text: 'songs'
            pos_hint: {'x':.8,'y':.8}
            size_hint: .05,.05
<Name>:
    FloatLayout:
        pos: 0,0
        size: root.width,root.height
        pos_hint_x: None
        pos_hint_y: None
        Label:
            id: nameOfSong
            text: "Choose a song"
            font_size: 20
            size_hint: None, None
            pos_hint: {'x': 0.435, 'y': 0.25}
        Label:
            id: artistOfSong
            text: ""
            font_size: 40
            size_hint: None, None
            pos_hint: {'x': 0.435, 'y': 0.20}

<SecondWindow>:
    name: "second"

PS: The code should: when you press next or last it should update the counter and the screen with the hi+counter(it's just a placeholder), after you click the button in the upper right corner it should list the numbers(also a placeholder usually it's songs and artists) and when you click on a number it should take you to the first screen(works until this point) and then change to label to the number you clicked

The number is also printed for debugging purposes

Leon Kardaš
  • 101
  • 8
  • 1
    If you want to call a method of a class without using a instance of the class, that suggests that that method should not be a method of the class, and should be a standalone function instead. – Carcigenicate Apr 30 '21 at 16:21
  • Please 1) try to create a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and 2) don't assume more than necessary about the solution (try to avoid [XY problems](https://xyproblem.info/)). – Karl Knechtel Apr 30 '21 at 16:23

0 Answers0