Nisarg
Nisarg

Reputation: 473

Run Another Class by Pressing Button in Kivy

I have written an app in kivy but It has total 3 classes one of them is application class second is exampleroot class and third is pscan calss which I want to run

Now when I press a button on screen .... So it should run pscan class....That's what I want...I have given my main.py and example.kv file below

MAIN.PY file

from kivy.app import App
import socket, sys, threading, os, time

################# LIBRARIES IMPORTED ##############


#####MY FIRST CLASS NAMED pscan ######

class pscan(threading.Thread):
    def __init__(self,ip, port):
        threading.Thread.__init__(self)
        self.ip = ip
            self.port = port
    def run(self):
        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

target = 'www.google.com'
sport = 1
eport= 100


############ MY SECOND CLASS NAMED EampleRoot ########


class ExampleRoot(BoxLayout):

    def final(self,*args):

        while sport <= eport:
            work=pscan(target,sport) #run pscan class from Exampleroot class
            work.start()             #DID not working
            time.sleep(0.1)
            sport=sport+1



class ExampleApp(App):

    return ExampleRoot()

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

.KV file

<Exampleroot>
        Button:
          text:"PRESS ME TO RUN PSCAN CLASS"
          on_press:root.final()

Upvotes: 0

Views: 754

Answers (1)

ZenCODE
ZenCODE

Reputation: 452

You App class needs to define a 'build' method which returns your root widget. Here is your code working, although I've no idea what it is trying to do..;-)

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

Builder.load_string('''
<Exampleroot>:
    Button:
        text:"PRESS ME TO RUN PSCAN CLASS"
        on_press:root.final()
''')


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

    def run(self):
        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):
        while self.sport <= self.eport:
            work = pscan(self.target, self.sport) #run pscan class from Exampleroot class
            work.start()             #DID not working
            time.sleep(0.1)
            self.sport = self.sport + 1


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

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

Upvotes: 2

Related Questions