1

I am fairly new to coding and most of the time I am researching as I go on how to write things. I am stumped at trying to get this label to update and I think it is because I am trying to change the StringVar() in a place I cannot.

Anyway the following is my code, sorry if it is ugly. I would appreciate any advice but most importantly I need the Label(connection_window, textvariable=isconnected).grid(row=3) to update when I change the StringVar() variable.

import socket
import sys
from Tkinter import *

root = Tk()


ip_entry = None
port_entry = None
isconnected = StringVar()

try:
    mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print ("failed to create socket")
    sys.exit()

def start_connection_window():
    connection_window = Toplevel(root)
    global ip_entry
    global port_entry
    global isconnected

    Label(connection_window, text = "Host IP Address:").grid(row=0)
    Label(connection_window, text = "Host Port #:").grid(row=1)

    ip_entry = Entry(connection_window)
    port_entry = Entry(connection_window)
    connect_button = Button(connection_window, text="connect", width=15, command=connect)

    ip_entry.grid(row=0, column=1)
    port_entry.grid(row=1, column=1)
    connect_button.grid(row=2, column=0, columnspan=2)
    Label(connection_window, textvariable=isconnected).grid(row=3)

def connect():
    global ip_address
    global port_number
    global isconnected
    isconnected = "worked"
    ip_address = ip_entry.get()
    port_number = port_entry.get()
    try:
        mysock.connect((ip_address,port_number))
        print("connected to",ip_address,port_number)
    except:
        isconnected.set("unable to connect")

open_connection = Button(root, text="Connection setup", width=15, height=3, command=start_connection_window)

Label(root, text = "Jason's Watering System", width=100).grid(row=0,column=0,columnspan=2)
open_connection.grid(row=0, column=2)

"""
mysock.sendall(message)
"""

mysock.close()
root.mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
JayEli
  • 13
  • 5
  • 2
    You're not just modifying `isconnected`; you're throwing it away and binding the reference to a completely different object, which is actually a string (`'worked'`) rather than a `StringVar()` object (so `isconnected.set()` should cause an error). That aside, you might want to look into [tracing](http://stackoverflow.com/questions/6548837/how-do-i-get-an-event-callback-when-a-tkinter-entry-widget-is-modified). – TigerhawkT3 Mar 02 '16 at 12:04

1 Answers1

2

First, by typing isconnected = "worked", you're re-binding isconnected to something other than the StringVar you set it to at the top. That's going to be a problem. You probably mean something like isconnected.set("worked").

Second, something like

mylabel = Label(connection_window, textvariable=isconnected)
mylabel.grid(row=3)
isconnected.trace("w", mylabel.update) # w = "changed"

will make it so the label is updated whenever the stringvar is changed.

acdr
  • 4,538
  • 2
  • 19
  • 45
  • 1
    I think you should address the issue in your answer that was said by Tigerhawk in comments. `isconnected = "worked"` this line destroys stringvar and makes it a string. – Lafexlos Mar 02 '16 at 12:10
  • You don't need the trace. The main feature of a `StringVar` is precisely that any widgets associated with it will automatically update. – Bryan Oakley Mar 02 '16 at 12:26