Python / Tkinter如何更新网格中的信息 [英] Python/Tkinter How to update information in grid

查看:175
本文介绍了Python / Tkinter如何更新网格中的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Python 3.2.2并编写一些代码来测试套接字。为了便于测试,我使用Tkinter来添加一个GUI界面。我还没有弄清楚的是如何更新我使用的网格中的信息。我想在下面的代码中更新函数change1和change3中的host2和port2:

 导入套接字
从tkinter导入*
导入tkinter.simpledialog
$ b $ root = Tk()
root.title(服务器)
root.iconbitmap (etc.icoico)
root.geometry(350x100 + 200 + 200)
frame = Frame(root)
host1 = Label(frame,text =Host:) .grid(row = 0,column = 0)
port1 =标签(frame,text =Port:).grid(row = 1,column = 0)
HOST ='localhost'
PORT = 11111
STATUS ='EMPTY'
host2 =标签(frame,text = HOST,width = 10).grid(row = 0,column = 1)
port2 = Label (frame,text = PORT,width = 10).grid(row = 1,column = 1)
status1 = Label(root,text = STATUS)
status1.pack(side = RIGHT,padx = 2,pady = 2)

def change1():
全局HOST
HOST = tkinter.simpledialog.askstring(title =主机,提示符=输入IP )
host2.grid_forget()
def change3():
全局PORT
PORT = tkinter.simpledial og.askinteger(title =Port,prompt =输入IP的端口号)
port2.grid_forget()
def go1():
全局HOST
全局PORT
home = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
home.bind((HOST,PORT))
home.listen(1)
conn, addr = home.accept()
print(addr)
while 1:
data = conn.recv(1024)
if not data:break
global STATUS
STATUS = data.decode('UTF-8')
conn.send(bytes('Received'Hello World'','UTF-8'))
conn.close()
全局状态1
status1.pack_forget()
status1.pack(side = RIGHT,padx = 2,pady = 2)

change = Button(frame,text = Change Host,width = 10,command = change1).grid(row = 0,column = 2)
change2 = Button(frame,text =Change Port,width = 10,command = change3)。 grid(row = 1,column = 2)
go = Button(frame,text =GO!,command = go1,width = 10).grid(row = 2,column = 2)
frame.pack(side = LEFT)

mainloop()

有关此事的任何帮助将不胜感激!感谢!

解决方案

您的问题始于此行:

  host1 = Label(frame,text =Host:).grid(row = 0,column = 0)

你正在做的是创建一个标签,使用网格将标签放在屏幕上,然后分配 host1 grid()命令的结果,它是空字符串。这使得以后不可能引用 host1 来获取对标签的引用。



相反,您需要保存对标签的引用。有了这个参考,你可以稍后改变任何你想要的标签:

  host1 =标签(frame,text =Host: )
host1.grid(row = 0,column = 0)
...
if(something_has_changed):
host1.configure(text =Hello,world!)

从拥有超过十年tk经验的人身上获取它,最好将您的小部件创建和布局。您的布局在开发过程中几乎肯定会发生变化,并且当您的所有布局代码位于同一位置时,这样做会更容易。我的布局可能会发生很大的变化,但我的工作集很少会这样做,所以我最终只需更改一个代码块,而不是与其他代码交错的几十行代码。



例如,我的代码一般看起来像这样:

  labell = tk.Label(...)
label2 = tk.Label(...)
entry1 = tk.Entry(...)

label1.grid( ...)
label2.grid(...)
entry1.grid(...)

当然,我使用更好的变量名称。

I'm running Python 3.2.2 and writing some code to test sockets. For the ease of testing, I'm using Tkinter to add a GUI interface. What I have yet to figure out is how to update the information in the grid I'm using. I want to update "host2" and "port2" in the functions "change1" and "change3" in the following code:

import socket
from tkinter import *
import tkinter.simpledialog

root = Tk()
root.title("Server")
root.iconbitmap("etc.ico")
root.geometry("350x100+200+200")
frame = Frame(root)
host1 = Label(frame,text="Host: ").grid(row=0,column=0)
port1 = Label(frame,text="Port: ").grid(row=1,column=0)
HOST = 'localhost'
PORT = 11111
STATUS = 'EMPTY'
host2 = Label(frame,text=HOST,width=10).grid(row=0,column=1)
port2 = Label(frame,text=PORT,width=10).grid(row=1,column=1)
status1 = Label(root,text=STATUS)
status1.pack(side=RIGHT,padx=2,pady=2)

def change1():
    global HOST
    HOST= tkinter.simpledialog.askstring(title="Host",prompt="Enter the IP of the Host.")
    host2.grid_forget()
def change3():
    global PORT
    PORT= tkinter.simpledialog.askinteger(title="Port",prompt="Enter the Port of the IP.")
    port2.grid_forget()
def go1():
    global HOST
    global PORT
    home = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    home.bind((HOST, PORT))
    home.listen(1)
    conn, addr = home.accept()
    print (addr)
    while 1:
        data = conn.recv(1024)
        if not data: break
        global STATUS
        STATUS = data.decode('UTF-8')
        conn.send(bytes('Received "Hello World"','UTF-8'))
    conn.close()
    global status1
    status1.pack_forget()
    status1.pack(side=RIGHT,padx=2,pady=2)

change = Button(frame, text="Change Host", width=10,command=change1).grid(row=0,column=2)
change2 = Button(frame, text="Change Port", width=10,command=change3).grid(row=1,column=2)
go = Button(frame, text="GO!",command=go1,width =10).grid(row=2,column=2)
frame.pack(side=LEFT)

mainloop()

Any help on the matter would be much appreciated! Thanks!

解决方案

Your problems begin with this line:

host1 = Label(frame,text="Host: ").grid(row=0,column=0)

What you are doing is creating a label, using grid to place the label on the screen, then assigning host1 the result of the grid() command, which is the empty string. This makes it impossible to later refer to host1 to get a reference to the label.

Instead, you need to save a reference to the label. With that reference you can later change anything you want about the label:

host1 = Label(frame, text="Host: ")
host1.grid(row=0, column=0)
...
if (something_has_changed):
    host1.configure(text="Hello, world!")

Take it from someone with over a decade of experience with tk, it's better to separate your widget creation and layout. Your layout will almost certainly change over the course of development and it's much easier to do that when all your layout code is in one place. My layouts may change a lot but my working set of widgets rarely does, so I end up only having to change one block of code rather than dozens of individual lines interleaved with other code.

For example, my code generally looks roughly like this:

labell = tk.Label(...)
label2 = tk.Label(...)
entry1 = tk.Entry(...)

label1.grid(...)
label2.grid(...)
entry1.grid(...)

Of course, I use much better variable names.

这篇关于Python / Tkinter如何更新网格中的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆