When I use the below code which display using .pack(), the number appeared. However when i change to place, there is no number appeared on the screen. How could I use place to display number instead of pack because I want to set it to specific location?
Code with pack() :
import tkinter as tk
master_window = tk.Tk()
master_window.geometry("250x150")
master_window.title("IntVar Example")
integer_variable = tk.IntVar(master_window, 255)
label = tk.Label(master_window, textvariable=integer_variable, height=250)
label.pack()
master_window.mainloop()
Code with place () :
import tkinter as tk
master_window = tk.Tk()
master_window.geometry("250x150")
master_window.title("IntVar Example")
integer_variable = tk.IntVar(master_window, 255)
label = tk.Label(master_window, textvariable=integer_variable, height=250)
label.place( x = 80 , y=80 )
master_window.mainloop()
How could I set the integer variable using place because i want it to display in specific location?
CodePudding user response:
You've set the label to be 250 characters tall. By default the text appears in the centered in the middle of the label. Because of the size it forces the value off screen. If you are able to make the window tall enough, you'll see the number.
If you remove the height
attribute from the label it will show up.
CodePudding user response:
To display the integer variable using place, you need to set the text attribute of the Label widget to the value of the IntVar variable. You can do this by using the get method of the IntVar object, like this:
label = tk.Label(master_window, text=integer_variable.get(), height=250)
label.place(x=80, y=80)
Alternatively, you can use a StringVar variable to store the value of the IntVar variable as a string, and then set the textvariable attribute of the Label widget to the StringVar variable. This will automatically update the label's text whenever the value of the IntVar variable changes.
string_variable = tk.StringVar(master_window)
label = tk.Label(master_window, textvariable=string_variable, height=250)
label.place(x=80, y=80)
# Update the StringVar variable with the value of the IntVar variable
string_variable.set(integer_variable.get())