Posts: 76
Threads: 25
Joined: Sep 2020
 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:
It did not stay position ?
Attached Files
Thumbnail(s)
Posts: 8,169
Threads: 160
Joined: Sep 2016
You create new Label every time you call the function in the loop
Posts: 76
Threads: 25
Joined: Sep 2020
Sep-17-2020, 10:48 AM
(This post was last modified: Sep-17-2020, 10:48 AM by ATARI_LIVE.)
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...
Posts: 8,169
Threads: 160
Joined: Sep 2016
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
Posts: 76
Threads: 25
Joined: Sep 2020
 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.
Posts: 8,169
Threads: 160
Joined: Sep 2016
note my post about not to use infinite loop
Posts: 150
Threads: 3
Joined: Apr 2020
(Sep-17-2020, 11:04 AM)ATARI_LIVE Wrote: 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.
Posts: 76
Threads: 25
Joined: Sep 2020
Sep-17-2020, 12:03 PM
(This post was last modified: Sep-17-2020, 12:03 PM by ATARI_LIVE.)
(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!
Posts: 6
Threads: 1
Joined: Sep 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)
Posts: 6,824
Threads: 20
Joined: Feb 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
|