pythonsocketstkinterirc

Python Tkinter: Resetting a previously defined variable with Entry


I'm trying to make it so that when a user enters their name, password, etc, it stores it globally so that the program is able to connect with that name. I've defined the variables at top like this:

global server
server = ""
global nick
nick = ""
global altnick
altnick = ""
global password
password = ""
global channel
channel = ""

Then, when the Tkinter program comes in, the user can use the Entries to enter all the proper values:

networktop = Toplevel(master=root)
networktop.title("Network List")
networktop.geometry("300x220")

Label(networktop, text="Nickname:").pack()
nickbox = Entry(networktop)
nickbox.grid(column="50", row="50")
nickbox.pack()

nick = nickbox.get()

Label(networktop, text="Alternate nick:").pack()
altbox = Entry(networktop)
altbox.grid(column="50", row="50")
altbox.pack()

altnick = altbox.get()

Label(networktop, text="Password:").pack()
pwbox = Entry(networktop, show="*")
pwbox.grid(column="50", row="50")
pwbox.pack()

password = pwbox.get()

Label(networktop, text="Channel to join:").pack()
chanbox = Entry(networktop)
chanbox.grid(column="50", row="50")
chanbox.pack()

channel = chanbox.get()

listvar = StringVar(networktop)
listvar.set("Choose a network...") # default value
listnetwork = OptionMenu(networktop, listvar, "irc.freenode.net")
listnetwork.config(width="50")
listnetwork.grid(column="50", row="50")
listnetwork.pack()

server = listvar.get()

networkconnect = Button(networktop, text="Connect", command=connect)
networkcancel = Button(networktop, text="Cancel", command=networktop.destroy)
networkcancel.pack(side=LEFT)
networkconnect.pack(side=RIGHT)

What I'm trying to achieve here is that when something is entered in (e.g. "NickName") it replaces nick = "" at top with nick = "NickName", so that when "Connect" is pressed it would connect to the server with "NickName". But I get the following error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
    return self.func(*args)
  File "C:\Users\dell\Desktop\irchat.py", line 29, in connect
    irc.connect((server, 6667))
  File "C:\Python27\lib\socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 10049] The requested address is not valid in its context

I feel like I'm being a total noob here and the solution is probably easy as pie. xD


Solution

  • It appears you are getting the value of the entry widget before the user has an opportunity to enter any text. You're creating the widget and then immediately getting the value. Because of this, server is the empty string and thus the socket code is failing.

    You don't want to get the value until the user has click the Connect button.