0

I cant make a label in tkinter. I found out that the label will only appear if i change pass to child.destroy()(in comments in code) I have no idea why the label is not appearing.

import tkinter as tk
from tkinter import *
from tkinter.ttk import *

def saved_page():
    for child in root.winfo_children():
        if child == nameF or menuF or infoF:
            pass                              # if i turn this pass into child.destroy() only then the label will appear

        else:
            child.destroy()

    infoL = Label(root, text = 'BlaBla')
    infoL.pack(pady = 100)

def home_page():
    global nameF, menuF, infoF

    root.overrideredirect(False)

    for child in root.winfo_children():
        child.destroy()

    # Frames
    nameF = tk.Frame(root, width = 900, height = 30)
    nameF.pack(fill = 'x', expand = False, side = 'top')
    menuF = Frame(root, width = 75, height = 500)
    menuF.pack(anchor = 'w')
    menuF.pack_propagate(0)
    infoF = Frame(root, width = 900 - 75, height = 500 - 30)
    infoF.pack()
    infoF.pack_propagate()

    nameL = Label(nameF, text = 'KM - Bot 5')
    nameL.pack(anchor = 'nw', padx = 10)

    homeB = Button(menuF, text = 'Home')
    recordB = Button(menuF, text = 'Record')
    savedB = Button(menuF, text = 'Saved', command = saved_page)
    updateB = Button(menuF, text = 'Update')
    settingB = Button(menuF, text = 'Settings')

    homeB.pack(pady = 10)
    recordB.pack(pady = 10)
    savedB.pack(pady = 10)
    updateB.pack(pady = 10)
    settingB.pack(fill = 'x', expand = False, side = 'bottom', pady = 10)

# starts the application intro
def start_intro():
    l1 = Label(root, text = 'This App')
    l1.pack()
    root.after(1000, home_page)

# seting up the window
root = Tk()
root.title('App')

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

w = 900
h = 500
x = (screen_width / 2) - (w / 2)
y = (screen_height / 2) - (h / 2)

root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.minsize(w, h)
#root.maxsize(w, h)

root.overrideredirect(True)

start_intro()
root.mainloop()
Dodu
  • 109
  • 2
  • 8
  • The `if child == nameF or menuF or infoF:` doesn't do what you think. Regardless, changing the `pass` to `child.destroy()` does not make the `Label` appear as you claim — it instead causes a `_tkinter.TclError: bad window path name ".!frame3"`. – martineau Jun 26 '22 at 12:51
  • I forgot to change where the Label appears, i meant to keep as `root` instead of `infoF`. Edited the question\ – Dodu Jun 26 '22 at 13:20
  • OK, but the `if child == nameF or menuF or infoF:` is still incorrect, not doing what you think, and likely the cause of the label issue (hint — it always evaluates to true). Did you even bother to read the answers to the indicated duplicate question? – martineau Jun 26 '22 at 13:56
  • Yes, i also googled in many ways about the question – Dodu Jun 26 '22 at 14:47
  • If you change the `pass` to `child.destroy()`, because the incorrect way the `if` statement is written, *all* the children of the root end up getting destroyed — and it is only then that you can see the `'BlaBla'` label. i.e. when it's the only thing in `root`. – martineau Jun 26 '22 at 14:54
  • Change the `if` statement to `if child in {nameF, menuF, infoF}:` and then the label will appear. – martineau Jun 26 '22 at 15:23
  • This does not work for me, also i use the `if child == nameF` in another script and it works. – Dodu Jun 26 '22 at 15:30
  • I managed to fix, it was about the positioning of the frames i guess as that fixed the issue, also i used the same if statement `if child == nameF or menuF:` – Dodu Jun 26 '22 at 16:05
  • What would be the difference as they both provide the same visual output – Dodu Jun 26 '22 at 16:20
  • That isn't quite the same `if` statement. Regardless, to properly compare the variable to multiple values in that case you should use `if child == nameF or child == menuF:`. Your comment indicates you *still* don't understand how boolean expressions work. It's important even if doing it wrong happens to produce the desired result because you (or someone else who knows how things work) may want to modify the code someday, and have correctly logic is important in at least that respect. – martineau Jun 26 '22 at 16:21

0 Answers0