Python Forum
Label.Place did not work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Label.Place did not work?
#1
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 ?

Attached Files

Thumbnail(s)
   
Reply
#2
You create new Label every time you call the function in the loop
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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...
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.
Reply
#6
note my post about not to use infinite loop
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(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.
Reply
#8
(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!
Reply
#9
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)
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to place global tk text widget in class or on canvas puje 1 574 Jul-04-2020, 09:25 AM
Last Post: deanhystad
  [Tkinter] How to place scroll bar correctly scratchmyhead 1 1,125 May-18-2020, 04:17 PM
Last Post: scratchmyhead
  How to use place holders in tkinter sqlite scratchmyhead 1 604 May-12-2020, 06:13 PM
Last Post: Larz60+
  thinker button gui place next to each other jacklee26 9 1,809 Jul-04-2019, 07:48 AM
Last Post: wuf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020