当我尝试在顶层添加按钮时出现以下错误。
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()
您无法创建按钮,win.Button
因为创建按钮不是通过Toplevel
方法完成的,而是使用 tkinter 类完成的。正确的语法是:
win = tk.Toplevel(root)
btn = tk.Button(win, text='fetch')
我在哪里使用了 import 语句import tkinter as tk
。通过这种方式,您可以清楚地看到 theToplevel
和 theButton
都是属于该tkinter
模块的类。创建按钮时,按钮的父项作为第一个参数给出。
另外,请注意text=
关键字参数不应大写。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句