Calling function on startup

hello,
when I try to call the function getSingleRows(), I get the following error
TypeError: Thirdwindow.getSingleRows() missing 1 required positional argument: ‘self’
please point out what I am missing

here are my files

main.py

import kivy


from kivy.config import Config
Config.set('graphics', 'width', '400')
Config.set('graphics', 'height', '500')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.image import Image
import sqlite3
from kivy.uix.screenmanager import ScreenManager,Screen
import random



    
        


class Firstwindow(Screen):
    pass
class Secondwindow(Screen):
    pass
class Thirdwindow(Screen):
    
    def getSingleRows(self):
            global x 
            x = random.randint(1,244)
            
            db = sqlite3.connect('book.db')
            cur = db.cursor()
            cur1= db.cursor()
            cur2 = db.cursor()
            cur3 = db.cursor()
            cur1.execute("select wronganswer1 from advanced")
            cur.execute("select questions from advanced")
            cur2.execute("select wronganswer2 from advanced")
            cur3.execute("select rightanswer from advanced")
            result1= cur1.fetchall()
            result2= cur2.fetchall()
            results= cur.fetchall()
            result3 = cur3.fetchall()
            self.ids.record.text = str (results[x])
            self.ids.l1.text = str (result1[x])
            self.ids.l2.text = str (result2[x])
            self.ids.l3.text = str (result3[x])
    getSingleRows()
                
            
                

    


class WindowManager(ScreenManager):
    def screen_manager_method(self):
        pass
class CrashCourse(App):
    def build(self):
        Builder.load_file("crashcourse.kv")
        return WindowManager()
    
if __name__ == '__main__':
    CrashCourse().run()

crashcourse.kv

<WindowManager>:
     Firstwindow:
     Secondwindow:
     Thirdwindow:
<Firstwindow>:
     canvas:
          Rectangle:
               source:"down.jpg"
               size: self.size
     name: "home_screen"
     id: home_screen
     Button:
          id: b1
          text: 'Beginner'
          color:"purple"
          pos:200,300
          size_hint:(0.2,0.1)
          on_release:
               app.root.current="game_screen"
          
     Button:
          id: b2
          text:" Inermediate"
          color:"purple"
          pos:200,230
          size_hint:(0.2,0.1)
     Button:
          id: b3
          text:"Advanced"
          color:"purple"
          pos:200,160
          size_hint:(0.2,0.1)
          on_release:
               app.root.current="menu_screen"

<Secondwindow>:
     name: "menu_screen"
     id: menu_screen
     Label:
          text:"this is screen two"
<Thirdwindow>:
     name: "game_screen"
     id: game_screen
     Label:
          id:record
          text:""
          pos_hint:{'x':0,'top':1.45}
          
     
     GridLayout
          
          padding:1
          cols:2
          
          Label:
               id:l1
               text:""
          CheckBox:
               group:"mygroup"
               
          Label:
               text:""
               id:l2
          CheckBox:
               group:"mygroup"
               on_active: root.checkbox_click(self,self.active,"second radio button")
          Label:
               text:""
               id:l3
          CheckBox:
               group:"mygroup"
               
          Button:
               
               text:"Move Next"
               on_press: root.getSingleRows()

getSingleRows is an instance method of Thirdwindow (it has a parameter self that will refer to the instance/object), but you’re calling it as a plain method getSingleRows(), so no instance there.

Hi,

if you want to call the method getSingleRows at start-up during an object instantiation, include an __init__ constructor method, and call the method in there, i.e:

def __init__(self):
    self.getSingleRows()

If you want access to the attribute x in other parts of your class script, define it in the constructor so that it becomes an attribute of the instances that you create. I don’t think that there is a need for the global keyword here.

where should I add this function
def init(self):
self.getSingleRows()

please be patient because I am new to this

At the top of the class, as in:

class Thirdwindow(Screen):

    def __init__(self):
         
        self.x = None  
        self.getSingleRows()
    
    def getSingleRows(self):

For the x value, assign it to whatever value is required during initialization. I have set it to None in this example since I do not know its intended initial value. For all classes, if you need code to run during instantiation, include it in the __init__ constructor method.

Hope that helps.

Experiment. For example:

class Some:

    def __init__(self):
        self.x = 10
        self.do_something()

    def do_something(self):
        print('\nHello, I am creating a new object when instantiated!')
        print("I have access to the value of 'x' from here: ", self.x)

obj1 = Some()  # Instantiate class to create new object
print("I also have access to 'x' from here: ", obj1.x)

As you can see, if you assign a variable in the __init__ constructor method, you can access it by any instance method or instance object. There is no need for a global keyword. However, if you wish to have access to an attribute between many instance objects, then assign it as a class attribute.

thank you for your patience but when I edited my code with the code
you provided it produced that says EXPECTED INDENTED BLOCK

class Firstwindow(Screen):
    pass
class Secondwindow(Screen):
    pass
class Thirdwindow(Screen):

    def __init__(self):
         
        self.x = None  
        self.getSingleRows()
    
    def getSingleRows(self):
             
            
                       
                

    

class WindowManager(ScreenManager):
    def screen_manager_method(self):
        pass
class CrashCourse(App):
    def build(self):
        Builder.load_file("crashcourse.kv")
        return WindowManager()
    
    
if __name__ == '__main__':
    CrashCourse().run()

The class word that defines Windowmanager(screen) has a red line
unerneath and when I hover it, it says expected indented block

When experimenting with new code and concepts , always start small. That is, begin with the bare minimum so that you can test the proposition in isolation. Thereafter, slowly build out.

In the script that you provided, the method getSingleRows(self) is missing a body statement. You can’t declare a method and leave it blank.

def getSingleRows(self):

add a statement to it:

def getSingleRows(self):
   print('Method statement.')  # Can be anything but for test purposes, added a print statement

Another way to think of this, is suppose that you are in a classroom. Your professor has a list/roster of all of the students that are attending that class. As he is taking roll call, he slowly looks at each name in the roster and links it to a student (a body). If at any point he comes upon a student that is missing (missing a body), he makes a note of it. This is essentially what is going on here. There is a method name that has been declared, but with a missing body. Anytime that you declare a method/function, it has to accompanied by a body statement. The same is true for classes.

1 Like

thank you the error has gone, but what about the code that deals
with the database? does it go inside def getSingleRows(self):?
thank you again for your advice

Yes, it can go inside the method. However, this is where your creativity will have to come into play. How do YOU want to design your application? As I stated in a previous post, start small. Experiment with a concept/propostion. Don’t write 2-3 pages worth of code and then decide to test for verification. Test at every step of the development phase so that you verify that it is being developed according to your requirements. This also helps you understand your own code btw.

Good luck!

1 Like

hello Paul again. I listed to your advice and started running the code for testing after adding print(“print something”), it produced this error

TypeError: Thirdwindow.init() got an unexpected keyword argument ‘__no_builder’

please, solve this problem