Python Forum
Receiving value is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Receiving value is not defined
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)
        con = s.connect((target,port))
        with print_lock:
            output=('port', port, 'is open!')

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

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

for worker in range(80,444):

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()


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.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020