nikol
nikol

Reputation: 33

add photo to a button

I've created a tkinter window with buttons, and I was able to put pictures on the buttons.

When the button is pressed 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()

Upvotes: 1

Views: 60

Answers (1)

vikas soni
vikas soni

Reputation: 598

You need to replace SGX = Tk() with SGX = Toplevel(). Tk usually represents 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

Upvotes: 2

Related Questions