-1

So this is the whole thing ...

from tkinter import *
from PIL import ImageTk,Image
import tkinter.font as f

root = Tk()
root.title("KBC")
root.configure(background="#8B008B")
root.geometry("1920x1080")
q="not started"
def start():
    head.place_forget()
    button.place_forget()
    global q
    q="started"

path="KK3O.jpg"
k = Image.open(path)
k = k.resize((1400, 800), Image.ANTIALIAS)
img = ImageTk.PhotoImage(k)
label = Label(root, image=img)
label.place(relwidth=1, relheight=1)


if q=="not started":
    #INTRODUCTION
    path = "K.png"
    k1 = Image.open(path)
    k1 = k1.resize((300,335), Image.ANTIALIAS)
    logo = ImageTk.PhotoImage(k1)
    head = Label(root, image=logo, borderwidth=10, relief="sunken")
    head.place(relx=0.5, rely=0.14, anchor='n')
    myfont = f.Font(family='Maiandra GD')
    button = Button(root, text="START", font=myfont, borderwidth=10, bg="#C5B358", command=start)
    button.place(relx=0.5, rely=0.68, relwidth=0.235, anchor='n')
else:
    #FRAME - 1A
    for i in range(1,6):
        label1 = Label(root, text="HEYYYYYYYYYYYYY",bg="red")
        label1.pack()
root.mainloop()

HOME PAGE IMAGE

THIS HAPPENS WHEN I CLICK START

These are the pictures of the home page, I was hoping that the '''label1''' widget would say "HEYYYYYYYYYYYYY" 5 times as soon as I press the start button , although both the button and the image disappear but the new label won't show up. How can I find the problem?

PS: I am still a beginner and this is my third project in tkinter.

braX
  • 11,506
  • 5
  • 20
  • 33
Salik Malik
  • 207
  • 1
  • 2
  • 15

2 Answers2

0

Put code from else to your function, so your code should look like:

from tkinter import *
from PIL import ImageTk,Image
import tkinter.font as f

root = Tk()
root.title("KBC")
root.configure(background="#8B008B")
root.geometry("1920x1080")
q="not started"
def start():
    head.place_forget()
    button.place_forget()
    global q
    q="started"
    for i in range(1,6):
        label1 = Label(root, text="HEYYYYYYYYYYYYY",bg="red")
        label1.pack()

path="KK3O.jpg"
k = Image.open(path)
k = k.resize((1400, 800), Image.ANTIALIAS)
img = ImageTk.PhotoImage(k)
label = Label(root, image=img)
label.place(relwidth=1, relheight=1)


if q=="not started":
    #INTRODUCTION
    path = "KK3O.jpg"
    k1 = Image.open(path)
    k1 = k1.resize((300,335), Image.ANTIALIAS)
    logo = ImageTk.PhotoImage(k1)
    head = Label(root, image=logo, borderwidth=10, relief="sunken")
    head.place(relx=0.5, rely=0.14, anchor='n')
    myfont = f.Font(family='Maiandra GD')
    button = Button(root, text="START", font=myfont, borderwidth=10, bg="#C5B358", command=start)
    button.place(relx=0.5, rely=0.68, relwidth=0.235, anchor='n')

root.mainloop()
  • Thank you soo much Alexey it works fine now, but i was hoping i could code the `label1` outside the function because i intend to add a lot more widgets and that might make things very complicated but thankyou for the suggestion very much appreciated – Salik Malik Mar 28 '20 at 13:55
-1

First thing first, when you use variables which are not defined in a function (like "head" and "button" in "start", you'd like to pass those as arguments or make them global and use the global keyword as you have done with "q".

Now, generaly speaking, when updating a widget you'd like to update your Tk object using the .update() method.

Tom Gil
  • 9
  • 2