-1

I am currently learning python and I am trying to get a tkinter button to pass variables from several entry fields to a function in another file. The button calls the scan function inside another module.

For some reason the function is getting executed as soon as I start and not on clicking the button.I dont understand why. Find button under # Start Scanning button I am hoping for your support

from tkinter import *
from PIL import Image, ImageTk
from Sockets_Portscanner_threaded import *


# Here, we are creating our class, Window, and inheriting from the Frame


# class. Frame is a class from the tkinter module. (see Lib/tkinter/__init__)
class Window(Frame):


    # Define settings upon initialization. Here you can specify
    def __init__(self, master=None):


        # parameters that you want to send through the Frame class. 
        Frame.__init__(self, master)   

        #reference to the master widget, which is the tk window                 
        self.master = master

        #with that, we want to then run init_window, which doesn't yet exist
        self.init_window()

    #Creation of init_window
    def init_window(self):

        # changing the title of our master widget      
        self.master.title("GUI")

        # allowing the widget to take the full space of the root window
        self.pack(fill=BOTH, expand=1)


        ####################
        #       FORM       #
        ####################

        #Grid labels
        Label(self, text="Please enter the Parameters For portscanning!").grid(row=0, column=1)
        Label(self, text="Server/domain").grid(row=2)
        Label(self, text="Ports:").grid(row=4, column=0)
        Label(self, text="From").grid(row=5)
        Label(self, text="To").grid(row=6)

        # creating a entry forms
        E1 = Entry(self)
        E2 = Entry(self)
        E3 = Entry(self)

        # placing entry forms in grid
        E1.grid(row=2, column=1)
        E2.grid(row=5, column=1)
        E3.grid(row=6, column=1)

        # Setting defaul variables for entry
        E1.insert(10,'localhost')
        E2.insert(10,'1')
        E3.insert(10,'500')
        ####################
        #       \FORM      #
        ####################

        # Start Scanning button
        Button(self, text='Show', command=scan(E1.get(),E2.get(),E3.get())).grid(row=7, column=1, sticky=W, pady=4)


    def client_exit(self):
        exit()



# root window created. Here, that would be the only window, but
# you can later have windows within windows.
root = Tk()

root.geometry("400x300")

#creation of an instance
app = Window(root)


#mainloop 
root.mainloop()  

The scan function in this button is in the Sockets_portscanner_threaded file

import socket
import threading
from queue import Queue

print_lock = threading.Lock()

#target = input('IP or domain of server to be scanned?')
#fromPort = input('From Port?')
#toPort = input('To port?')
portlist = []


def portscan(port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        con = s.connect((target,port))
        with print_lock:
            print('port',port,'is open!')
            portlist.append(port)
            print(portlist)
        con.close()
    except:
        pass
def threader():
    while True:
        worker = q.get()
        portscan(worker)
        q.task_done()


q = Queue()
def scan(target,fromPort,toPort):
    print('SCAN() execute')
    for x in range(100):
        t = threading.Thread(target=threader)
        t.daemon = True
        t.start()

    for worker in range(int(fromPort),int(toPort)):
        q.put(worker)

    q.join()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
veritaS
  • 511
  • 1
  • 5
  • 23

1 Answers1

2

you can try using a lambda.

try using:

Button(self, text='Show', command= lambda: scan(E1.get(),E2.get(),E3.get()) )

This will pass the information in the event into the button.

You can use this for reference. https://pythonprogramming.net/passing-functions-parameters-tkinter-using-lambda/

hope it helps

cahg88
  • 36
  • 4