简体   繁体   English

Python Tkinter StringVar()问题

[英]Python Tkinter StringVar() issues

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. 我为尝试更新此标签而感到困惑,我认为这是因为我试图在无法更改的地方更改StringVar()

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. 我将不胜感激任何建议,但最重要的是Label(connection_window, textvariable=isconnected).grid(row=3)当我更改StringVar()变量时Label(connection_window, textvariable=isconnected).grid(row=3)我需要Label(connection_window, textvariable=isconnected).grid(row=3)进行更新。

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

First, by typing isconnected = "worked" , you're re-binding isconnected to something other than the StringVar you set it to at the top. 首先,通过键入isconnected = "worked" ,您将重新isconnected到除顶部设置的StringVar之外的其他东西。 That's going to be a problem. 那将是一个问题。 You probably mean something like isconnected.set("worked") . 您可能会说像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. 将使它,以便在更改stringvar时更新标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM