Python / Tkinter How to update grid information

I am running Python 3.2.2 and writing code for socket testing. For testing convenience, I use Tkinter to add a GUI. I have yet to figure out how to update the information in the grid used. 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 this would be greatly appreciated! Thanks!

+6
source share
2 answers

Your problems begin with this line:

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

What you do is create a label using the grid to place the label on the screen, and then assign host1 result of the grid () command, which is an empty string. This makes it impossible for a later reference to host1 to get a reference to the label.

Instead, you need to save the link to the label. Using this link, you can later change everything you want by 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 more than ten years of experience with tk, it is better to separate the creation and layout of the widget. Your layout will almost certainly change during the development process, and it's much easier to do when all of your layout code is in one place. My layouts can change a lot, but my working set of widgets is rarely executed, so I can only change one block of code, and not dozens of separate lines alternating with other code.

For example, my code looks something 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.

+11
source

First, before delving into this problem. I want to highlight a few things. In this line.

 host2 = Label(frame,text=HOST,width=10).grid(row=0,column=1) 

I want you to separate part of the grid from a variable declaration. Because it will create a No Type object that you cannot work with. In the future, this will create many problems that can take a long time. If you have any variables structured this way that won't just serve as lines of text. Change the structure of this variable to the structure that I described above. In any case, returning to what you say, but in more detail you can change the text of the labels. What would I do if the function is changed by the function in the function that you want to change in the label text. Put in a line like this.

 host2['text'] = 'Your New Text' port2['text'] = 'Your New Text' # or host2.configure(text = 'Your New Text') port2.configure(text = 'Your New Text') 

This will change the text of your shortcuts to the new changed text, or in other words, replace the text with new text.

0
source

Source: https://habr.com/ru/post/905787/


All Articles