Nisarg
Nisarg

Reputation: 473

Can not access ids of .kv file from main.py file in kivy

I have main.py as below :

I have two clss and one of them is pscan which basically do a process

and then do another process using loops

but i can not access any attributes to another class using pscan class

it always gives me error like

class pscan has no attributes 'pb'

from kivy.app import App
import socket, sys, threading, os, time
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout


class pscan(threading.Thread):
    
    def __init__(self,ip, port):
        threading.Thread.__init__(self)
        self.ip = ip
        self.port = port

    def run(self):
        self.ids["pb"].value=self.ids["pb"].value+1    #Increasing progress bar
#Which is not working on this level it give me erro


        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.settimeout(4)
            s.connect((socket.gethostbyname(self.ip), int(self.port)))
            print "\n[+] OPEN:",self.ip, self.port
            s.close()
        except:
            print "\n[!] CLOSE:",self.ip, self.port

  


class ExampleRoot(BoxLayout):
    sport = 1
    target = 'www.google.com'
    eport= 100

    def final(self,*args):
        self.ids["pb"].max=int(self.eport)   ##defining size of progress bar
        while self.sport <= self.eport:
            work = pscan(self.target, self.sport) 
            work.start()            
            time.sleep(0.1)
            self.sport = self.sport + 1


class ExampleApp(App):
    def build(self):
        return ExampleRoot()

if __name__ == "__main__":
    ExampleApp().run()

Here is my example.kv file :

<Exampleroot>
  
  BoxLayout:
  
    Button:
      text:"PRESS ME TO RUN PSCAN CLASS"
      on_press:root.final()
  
    ProgressBar:     ### I want this increasing using main.py when each port get scan  using pscan class
      id:pb
      max:0
      value:0

any solution guys ??

Upvotes: 0

Views: 264

Answers (1)

Totem
Totem

Reputation: 7369

You are trying to access self.ids["pb"] from the pscan class. But just like the error says, the pscan class doesn't have an attribute called pb. You defined the widget with the id 'pb' in the ExampleRoot class in the .kv file.

I am a bit confused, because you never explicitly call the pscan classes run() method(Did you mean to override the start method?). However, if you want the progress bars value to be incremented each time you start a new thread, you could do that inside the while loop in ExampleRoot, and shouldn't receive that particular error anymore, as the widget with id 'pb' exists inside of ExampleRoot.

while self.sport <= self.eport:
    work = pscan(self.target, self.sport) 
    work.start()
    self.ids.pb.value += 1    # <<< try it here        
    time.sleep(0.1)
    self.sport = self.sport + 1

Upvotes: 1

Related Questions