Python Forum
Label.Place did not work? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Label.Place did not work? (/thread-29718.html)

Pages: 1 2


Label.Place did not work? - ATARI_LIVE - Sep-17-2020

Huh Strange, the label Place I know of code is right but not working... I might missed something:
from tkinter import *
import time

win = Tk()

win.attributes('-fullscreen', True)
win.configure(bg='black')

def start_display(V):
    lab1=Label(win, text=V, font=('DIGITAL DISMAY',50),fg="blue", bg="black")
    lab1.place(x=10,y=10)
    lab1.pack()
    return

while True:
    T = time.strftime("%H:%M:%S")
    start_display(T)
    time.sleep(1)
    win.update()

win.mainloop()
The result is:
[Image: 1PGB545HJk8201WEBcetwngQGrVhoC6wG]

It did not stay position ?


RE: Label.Place did not work? - buran - Sep-17-2020

You create new Label every time you call the function in the loop


RE: Label.Place did not work? - ATARI_LIVE - Sep-17-2020

Okay, how about this?
from tkinter import *
import time

win = Tk()
win.attributes('-fullscreen', True)
win.configure(bg='black')

T = StringVar()
T.set(time.strftime("%H:%M:%S"))

lab1=Label(win, textvariable=T, font=('DIGITAL DISMAY',50),fg="blue", bg="black")
lab1.place(x=100,y=100)
lab1.pack()

while True:
    P=time.strftime("%H:%M:%S")
    T.set(P)
    time.sleep(1)
    win.update()

win.mainloop()

Ah it's worked but the position did not set hummm...


RE: Label.Place did not work? - buran - Sep-17-2020

To expand on my previous answer, because there is same issue present here.
when you create infinite loop like the one on lines 15-16 you block the execution of the program further. i.e. you never reach/start tkinter mainloop on line 18

you need to define function and call it periodically using Tk..after() method

from tkinter import *
import time
 
win = Tk()
win.attributes('-fullscreen', True)
win.configure(bg='black')
 
T = StringVar()
T.set("88:88:88")
 
lab1=Label(win, textvariable=T, font=('DIGITAL DISMAY', 50), fg="blue", bg="black") # 
lab1.place(x=10,y=10)
lab1.pack()
 
def update_clock():
    T.set(time.strftime("%H:%M:%S"))
    win.after(1000, update_clock)
 
update_clock()
win.mainloop()
no need to say that it will be better if you use class

https://stackoverflow.com/a/459131/4046632


RE: Label.Place did not work? - ATARI_LIVE - Sep-17-2020

Dance DANG! I found it use:
lab1=Label(win, textvariable=T, font=('DIGITAL DISMAY',50),fg="blue", bg="black").place(x=100, y=100)
.
its worked.


RE: Label.Place did not work? - buran - Sep-17-2020

note my post about not to use infinite loop


RE: Label.Place did not work? - GOTO10 - Sep-17-2020

(Sep-17-2020, 11:04 AM)ATARI_LIVE Wrote: Dance DANG! I found it use:
lab1=Label(win, textvariable=T, font=('DIGITAL DISMAY',50),fg="blue", bg="black").place(x=100, y=100)
.
its worked.

One thing to be aware of with this code, your variable lab1 is going to have a value of None since it is getting the return value of the place() method. Not a problem if you never want to manipulate the label again, but in that case you don't need the variable at all.


RE: Label.Place did not work? - ATARI_LIVE - Sep-17-2020

(Sep-17-2020, 11:05 AM)buran Wrote: note my post about not to use infinite loop

Okay, sorry for my pain in the butt... what code should be without infinite loop?
Have to add IF press ESC to exit?

Nevermind, I get it. sorry.
Now, I understood about loop.. yeah my code was stuck loop in the while and never go to mainloop. THANK YOU!


RE: Label.Place did not work? - TAREKYANGUI - Sep-17-2020

Hi,
or juste eliminate lab1.pack() in raw13 and adjust place in row12 to lab1.place(x=100, y=100)instead of lab1.place(x=10, y=10)


RE: Label.Place did not work? - deanhystad - Sep-17-2020

GUI programming is confusing. A lot of new GUI programmers think they need to wait for buttons to be pressed or text to be entered or for time to pass between updating values in the window. They are used to the code dictating what the program does.

This is not how GUI programming done. GUI programming is event based. The user of the program dictates what the program does. You write actions that are executed in response to user events. In tkinter this is done by specifying a function that is called when a button is pressed or a StringVar (IntVar...) value is changed. Periodic updates are done using "after" to call a function after some time has passed. Nowhere in your program can you have a loop that runs forever, and all the action code should be writtin to take very little time or the GUI is unresponsive and feels buggy. It is completely backward from the kind of code you are used to writing