Home > Enterprise >  add photo to a button
add photo to a button

Time:09-30

I've created a tkinter window with buttons,

I was able to put pictures on the buttons.

When you press the button I set it to open another window where I also put buttons,

but can not put a picture on them.

when I add the picture the buttons disappear

The code:

from tkinter import *

root = Tk()

root.title('GARBIL.TK')

p1 = PhotoImage(file='03.png')

root.iconphoto(False, p1)

canvas_width = 1900

canvas_height = 1080

w = Canvas(root, width=canvas_width, height=canvas_height)

w.pack()

serveron = PhotoImage(file="01.PNG")

serveroff = PhotoImage(file="02.PNG")

def SGX():

 SGX = Tk()

 SGX.title('server map')

 SGX.geometry("900x700")

 SGX21 = Button(SGX, text='SGX.21', bg='#008080', fg='#ffffff', compound=BOTTOM)

 SGX21.place(x=200, y=250)

SGXon = Button(root, text=' SGX ', image=serveron, command=SGX, compound=BOTTOM)

SGXon.place(x=1080, y=100)

mainloop()

CodePudding user response:

You need to replace SGX = Tk() as SGX = Toplevel(). Tk represents mostly the main window of an application. For the sub-window, you need to use Toplevel instead.

SGX = Toplevel()

so your SGX should look like this:

def SGX():

 SGX = Toplevel()

 SGX.title('server map')

 SGX.geometry("900x700")

 SGX21 = Button(SGX, text='SGX.21', image=p1, bg='#008080', fg='#ffffff', compound=BOTTOM)

 SGX21.place(x=200, y=250)

enter image description here

  • Related