Python Forum
Receiving value is not defined - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Receiving value is not defined (/thread-13554.html)



Receiving value is not defined - cibb - Oct-20-2018

In my script it runs fine without adding the GUI but trying to add tkinter I receive
line 44, in <module>
button_1=Button(my_window,text="Check Ports", command=portscan (port))
NameError: name 'port' is not defined

Port is defined within my function and the error only appears when I've added tkinter GUI to the code.

Again the code runs just fine without tkinter but when I add tkinter it gives the error.I'm trying to have a user entry box and use that variable as a target for a port scanner. The button would initiate my function to perform the port scan.


My next phase will be have this output in a secondary window that will popup but at present I'm just trying to get the basics working with tkinter from the input side.

from tkinter import *
import socket
import threading
from queue import Queue

print_lock = threading.Lock()



def portscan (port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        con = s.connect((target,port))
        with print_lock:
            output=('port', port, 'is open!')
    except:
        pass

def threader():
    while True:
        worker = q.get()
        portscan(worker)
        q.task_done()


q = Queue()
for x in range(30):
    t = threading.Thread(target=threader)
    t.daemon = True
    t.start()

for worker in range(80,444):
    q.put(worker)






my_window = Tk()

label_1 = Label(my_window, text="Server IP or Host Name")
entry_1 = Entry(my_window)
button_1=Button(my_window,text="Check Ports", command=portscan (port))


label_1.grid(row=0, column=0)
entry_1.grid(row=0, column=1)
button_1.grid(row=1, column=0)

target = entry_1.get()

q.join()

root.mainloop()



RE: Receiving value is not defined - ichabod801 - Oct-20-2018

The command parameter in tkinter objects is a callable: It is called when the button is clicked. But you're assigning portscan(port) to it, which is evaluated before being assigned. Even if you weren't getting the NameError, it would put None in the command parameter, because that's what portscan returns.

This Stack Overflow question has answers with several ways to pass a parameter to a callable assigned to a tkinter command paramteer.