Home > database >  Button not showing up while opening new page (python tkinter)
Button not showing up while opening new page (python tkinter)

Time:06-13

I want to use the listbox in tkinter in order to open a new window with a button, but when I'm pressing a message it only opens the window, without the button.

from tkinter import *
from playsound import playsound
import os
# create a window as root using Tk() function
root = Tk()
root.geometry("200x200")
# create a changecolor function
# to change background color of window

def new_win():
    top2 = Toplevel(root)
    top2.geometry('200x200')
    top2.title("display Window")
    # photo2 = PhotoImage(file=r"hearing.png")
    button = Button(text='wtf')

def changecolor(event):
    # get selected list box color item
    new_win()

# create listbox
listbox = Listbox(root , font=('times 20 bold'), height=5,width=10)
# insert color items into listbox
listbox.insert(1, 'first')
listbox.insert(2, 'second')
listbox.insert(3, 'third')
listbox.insert(4, 'forth')
listbox.pack(pady=20)
# bind the click event with listbox and
# call changecolor() function
listbox.bind('<<ListboxSelect>>', changecolor)
root.mainloop()

Any suggestion would be very helpful.

CodePudding user response:

You can change the line 16
button = Button(text='wtf')
to

button = Button(top2,text='wtf')
button.pack()

CodePudding user response:

B = Button(top, text ="Hello", command="enter command here")

B.pack()

try adding the button to your code but outside the function

  • Related