'How to change a value in an existing window and display it while it changes
import random
while 1:
x = randomint(0,25)
Print(x)
Is it possible to Print the "x" Output in a tkinter GUI (just changing the value in the existing window without opening a new Window for a new value)?
I know how to create windows and show text within the window. But I don't understand how to show the value in the window even when the value is changing
Solution 1:[1]
If you want to update a value in tkinter, you could use a label and then use the configure (or config) method to modify the value every time the variable changes.
Illustrative Code:
from tkinter import *
def update_value(value):
global label
label.configure(text = value)
root = Tk()
#Creating a frame to contain a label and an entry
frame = Frame(root)
frame.grid(row = 0, column = 0, padx = 5, pady = 5, sticky = NSEW)
#Creating the label
label = Label(frame, text = "Label", bg = "black", fg = "white", font = ('Comic Sans', 15))
label.grid(row = 0, column = 0, padx = 5, pady = 5, ipadx = 5, ipady = 5, sticky = EW)
#Creating an entry
entry = Entry(frame, font = ('Comic Sans', 15), bg = "white", fg = "black")
entry.grid(row = 0, column = 1, padx = 5, pady = 5, ipadx = 5, ipady = 5, sticky = EW)
#Adding a key binding to entry so that everytime the user hits the enter key, the updated value shows in the label
entry.bind("<Return>", lambda e: update_value(entry.get()))
root.mainloop()
Every time the user makes a change in the entry box and hits the enter key, the label gets updated with the new value in the entry box.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sriram Srinivasan |
