Home > Net >  How do i get image under the text In Tkinter?
How do i get image under the text In Tkinter?

Time:03-31

I recently started learning python and Tkinter. I started about a month ago. But let's get to the point. I been making a random software (so dont mock), for 1 day now, and the code is ready, but i still need to make the app itself, so i been doing it, but face a problem, when making background changing buttons. I made buttons which change the background either to a colour or a picture.

Well the normal colours work really well, but the image seem to be acting weird. I got the image appearing from the button, but the problem is, when i ckick the button, the image displays OVER the texts, entry ect.

"Code Fix" that works for non-changing picture -->

Tkinter.Label(window, i=bgimg, compound=Tkinter.CENTER).pack()

I havent seen many people have this problem. I saw one, and that worked (if i would have a non-changeable background), but because i have the buttons the code just doesn't care about it. (The code that would have helped for a non-changing picture.)

Please someone help, and maybe add me on Discord because i think im goint to need help later on too :)

oh and here is the code ->

import tkinter as tk
import tkinter


window=Tk()
bgimg= tk.PhotoImage(file = "C:\\Users\\Hoze\\Downloads\\Hoze (1).png")




btn=Button(window, text="Convert", fg='blue', padx=50)
btn.place(x=220, y=380)

def myClick():
    myLabel = Label(window, window.configure(bg='red'))
    myLabel.pack()
btn=Button(window, text="Red", fg='red', padx=20, command=myClick)
btn.place(x=520, y=150)

def myClick():
    myLabel = Label(window, window.configure(bg='blue'))
    myLabel.pack()
btn=Button(window, text="Blue", fg='blue', padx=19, command=myClick)
btn.place(x=520, y=200)

def myClick():
    myLabel = Label(window, window.configure(bg='green'))
    myLabel.pack()
btn=Button(window, text="Green", fg='green', padx=15, command=myClick)
btn.place(x=520, y=300)

def myClick():
    myLabel = Label(window, window.configure(bg='orchid'))
    myLabel.pack()
btn=Button(window, text="Default", fg='orchid', padx=11, command=myClick)
btn.place(x=520, y=250)


#Here is the problem, and here is the code that put the picture like it should UNDER the text (if i put the code to the start)(But then it does't change to the other colours)
def myClick():
    myLabel= Label(window, i=bgimg, )
    myLabel.pack()
btn=Button(window, text="Cool", fg='aqua', padx=11, command=myClick)
btn.place(x=520, y=250)
#If i now run this code, the picture will come on top of the text. I hope yall get it, im kinda new
#

lbl=Label(window, text="  Extract a word here, \n that you want make secret!", fg='blue', font=("Helvetica", 16))
lbl.place(x=166 , y=50)
lbl=Label(window, text="Your Secret Word is: ", fg='blue', font=("Helvetica", 10))
lbl.place(x=120, y=300)

txtfld=Entry(window, text="This is Entry Widget", bd=5)
txtfld.place(x=234, y=150)

window.title('Hoze Secret Lenguage Converter')
window.geometry("600x500 10 10")
window.minsize(600, 500)
window.maxsize(600, 500)
window.iconbitmap("C:\\Users\\Hoze\\Music\\Coding\\Project Secret\\favicon.ico")
window.configure(bg='green')

window.mainloop() 

CodePudding user response:

This should make your stuff

#import tkinter as tk
from email.mime import image
from tkinter import *


window=Tk()
bgimg= PhotoImage(file = "C:\\Users\\Hoze\\Downloads\\Hoze (1).png")
myLabel= Label(window, i=bgimg )
myLabel.pack()
myLabel.pack_forget()

btn=Button(window, text="Convert", fg='blue', padx=50)
btn.place(x=220, y=380)




def myClick():
    myLabel.pack_forget()
    window.configure(bg='red')

btn=Button(window, text="Red", fg='red', padx=20, command=myClick)
btn.place(x=520, y=150)

def myClick():
    myLabel.pack_forget()
    window.configure(bg='blue')
    
btn=Button(window, text="Blue", fg='blue', padx=19, command=myClick)
btn.place(x=520, y=200)

def myClick():
    myLabel.pack_forget()
    window.configure(bg='green')
    
btn=Button(window, text="Green", fg='green', padx=15, command=myClick)
btn.place(x=520, y=300)

def myClick():
    myLabel.pack_forget()
    window.configure(bg='orchid')

    
btn=Button(window, text="Default", fg='orchid', padx=11, command=myClick)
btn.place(x=520, y=250)


#Here is the problem, and here is the code that put the picture like it should UNDER the text (if i put the code to the start)(But then it does't change to the other colours)
def myClick():
    myLabel.pack()

btn=Button(window, text="Cool", fg='aqua', padx=11, command=myClick)
btn.place(x=520, y=250)
#If i now run this code, the picture will come on top of the text. I hope yall get it, im kinda new
#

lbl=Label(window, text="  Extract a word here, \n that you want make secret!", fg='blue', font=("Helvetica", 16))
lbl.place(x=166 , y=50)
lbl=Label(window, text="Your Secret Word is: ", fg='blue', font=("Helvetica", 10))
lbl.place(x=120, y=300)

txtfld=Entry(window, text="This is Entry Widget", bd=5)
txtfld.place(x=234, y=150)

window.title('Hoze Secret Lenguage Converter')
window.geometry("600x500 10 10")
window.minsize(600, 500)
window.maxsize(600, 500)
window.iconbitmap("C:\\Users\\Hoze\\Music\\Coding\\Project Secret\\favicon.ico")
window.configure(bg='green')

window.mainloop() 
  • Related