Python/Tkinter How to update information in grid Python/Tkinter How to update information in grid tkinter tkinter

Python/Tkinter How to update information in grid


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.


First, before going in depth with this problem. I want to highlight out a few things. In this line.

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

I want you to separate the gridding part from the declaration of the variable. Because it will create a No Type object that you cant work with. It will create many problems in the future that may take a lot of your time. If you have any variables structured like this that are not just going to be served as lines of text. Change the structure of that of that variable to the structure that I described above. Anyways, going back to what you where saying but in more depth is that to change the text of the Labels. What I would do if its going to be changed by function is in that function that you are wanting to change the text of the Label. Put in lines like this.

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

This will change the text of your labels to the newly changed text or In other words replaces the text with the new text.