Python Forum
Trouble with Tkinter labels
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with Tkinter labels
#4
This does not draw the label.
        down_inst = Label(root, text='And Installing ' + ck_folder, font=('Arial', 15))
        down_inst.place(x=150, y=450)
This draws the label
root.mainloop()
You can see this if you run the program below. We force the window to update after making the first label, but the second label waits for mainloop() to update the root window.
import tkinter as tk
from time import sleep

root = tk.Tk()
tk.Label(root, text="How many labels", font=(None, 64)).pack()
root.update()
tk.Label(root, text="do you see?", font=(None, 64)).pack()
sleep(5)
root.mainloop()
And before you start adding update() commands all over the place, stop and think about what you are using tkinter for. If you block mainloop() from running the window stops responding to user actions (button clicks, typing) and the window stops updating, even if you make request to change the windows appearance. Do you want your window to go dead while the program is busy. Your users will not be impressed.

Are you blocking mainloop() from running? Is there something that takes a long time to run and this prevents your window from updating? If so, that code should be run in a separate thread.

This is not the correct way to modify a label.
        down_inst = Label(root, text='Downloading ' + ck_folder + ' from archive', font=('Arial', 15))
        down_inst.place(x=100, y=420)
        root.after(5000, down_inst.destroy)  # 1000=1 second
        shutil.copyfile(dw_source_path + dw_file_name, td_dest_path + dw_file_name)
 
        # ---- installing program ----
        down_inst = Label(root, text='And Installing ' + ck_folder, font=('Arial', 15))
        down_inst.place(x=150, y=450)
You should hardly ever use destroy(). Make the window and labels at the start of run. To display a different message, change the label text. The example below uses a tk.StringVar to change the label text.
import tkinter as tk
from time import sleep


class Window(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.message = tk.StringVar()
        label = tk.Label(self, textvariable=self.message, width=60, font=(None, 32))
        label.pack(padx=10, pady=10)

    def display_message(self, message_list, delay=10):
        if message_list:
            self.message.set(message_list[0])
            self.after(delay * 1000, self.display_message, message_list[1:], delay)
        else:
            self.message.set("")


window = Window()
window.display_message(
    (
        "Twas the night before Christmas, when all through the house",
        "Not a creature was stirring, not even a mouse",
        "The stockings were hung by the chimney with care",
        "In hopes that St. Nicholas soon would be there",
    ),
    delay=3,
)
window.mainloop()
You should not use place() to set the location of a widget. Use pack() or grid(). What if you have a user that selected a larger system font? Your labels will resize, but are they correctly spaced? Using pack() or grid() lets tkinter adjust the window to fit the contents. It also lets you easily make resizable windows.
Reply


Messages In This Thread
Trouble with Tkinter labels - by Raysz - Sep-09-2023, 11:18 PM
RE: Trouble with Tkinter labels - by menator01 - Sep-10-2023, 12:31 AM
RE: Trouble with Tkinter labels - by Raysz - Sep-10-2023, 08:34 AM
RE: Trouble with Tkinter labels - by deanhystad - Sep-10-2023, 03:46 PM
RE: Trouble with Tkinter labels - by Raysz - Sep-10-2023, 06:02 PM
RE: Trouble with Tkinter labels - by deanhystad - Sep-10-2023, 04:59 PM
RE: Trouble with Tkinter labels - by deanhystad - Sep-11-2023, 02:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Trouble changing Font within tkinter frame title AnotherSam 1 4,264 Sep-30-2021, 05:57 PM
Last Post: menator01
  [Tkinter] modify the html table to include data from tkinter labels rwahdan 0 1,655 Jun-20-2021, 10:33 AM
Last Post: rwahdan
  Tkinter having problems with packing labels? wallgraffiti 0 1,579 Aug-02-2020, 09:26 AM
Last Post: wallgraffiti
  Creating and destroying dynamic labels in Tkinter MarcusRoberts 1 4,402 May-02-2020, 06:49 PM
Last Post: Yoriz
  Spacing Between two labels are very far and Top label is not in Center using Tkinter barry76 2 7,193 Jul-30-2019, 10:49 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