Mohua Das Gupta
Mohua Das Gupta

Reputation: 13

How to add a button in tkinter toplevel() window

I am getting below error when I am trying to add button in toplevel.

AttributeError: 'Toplevel' object has no attribute 'Button' 

Part of Code:

def open_window():  
    win=Toplevel(root)  
    win.geometry("400x400")
    win.title("Table Related Information")
    win.grab_set() 
    btn=win.Button(topframe,Text="Fetch")
    btn.pack()

Upvotes: 1

Views: 1425

Answers (1)

j_4321
j_4321

Reputation: 16169

You cannot create the button with win.Button because creating a button is not done through a Toplevel method but with a tkinter class. The correct syntax is:

win = tk.Toplevel(root)
btn = tk.Button(win, text='fetch')

where I used the import statement import tkinter as tk. This way you clearly see that both the Toplevel and the Button are classes belonging to the tkinter module. The parent of the button is given as the first argument when you create it.

Also, notice that the text= keyword argument should not be capitalized.

Upvotes: 1

Related Questions