如何在tkinter toplevel()窗口中添加按钮

时间:2018-12-14 09:03:26

标签: python tkinter

当我尝试在顶层添加按钮时,出现错误提示。

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

部分代码:

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()

1 个答案:

答案 0 :(得分:1)

您不能使用win.Button创建按钮,因为创建按钮不是通过Toplevel方法完成的,而是通过tkinter类完成的。正确的语法是:

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

我在其中使用导入语句import tkinter as tk。这样,您可以清楚地看到ToplevelButton都是属于tkinter模块的类。创建按钮时,该按钮的父级将作为第一个参数。

另外,请注意,关键字text=不应大写。

相关问题