无法从kivy

时间:2015-08-06 06:15:12

标签: android class python-2.7 kivy

我的main.py如下:

我有两个clss,其中一个是pscan,基本上是一个过程

然后使用循环

执行另一个过程

但我无法使用pscan类

访问另一个类的任何属性

它总是给我一个像

这样的错误
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()

这是我的example.kv文件:

<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

任何解决方案的家伙?

1 个答案:

答案 0 :(得分:1)

您正尝试从self.ids["pb"]课程访问pscan。但就像错误所说的那样,pscan类没有名为pb的属性。您在.kv文件的ExampleRoot类中定义了名为“pb”的小部件。

我有点困惑,因为您从未明确调用pscanrun()方法(您是否要覆盖start方法?)。但是,如果您希望每次启动新线程时都会增加进度条值,则可以在ExampleRoot中的while循环内执行此操作,并且不应再接收该特定错误,因为具有id的窗口小部件'pb'存在于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