tkinter列表框错误-AttributeError:"int"对象没有属性"tk" [英] tkinter listbox error - AttributeError: 'int' object has no attribute 'tk'

查看:54
本文介绍了tkinter列表框错误-AttributeError:"int"对象没有属性"tk"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 tkinter 的新手,正在尝试制作一个列表框.这是我正在使用的代码,我不断收到错误AttributeError:'int'对象没有属性'tk'.我在做什么错了?

I am new to tkinter and trying to make a listbox. This is the code I am using, I keep getting the error AttributeError: 'int' object has no attribute 'tk'. What am I doing wrong?

from tkinter import *

def ListWindow():
    Listwindow = Tk()           
    Listwindow.title("Welcome")
    Listwindow.geometry("400x130")

    lbl_welcome = Label(Listwindow,text="Welcome to A list box!")
    lbl_welcome.grid(row=0,column=0,columnspan=10)

    myList = Listbox(Listwindow)
    myList.grid(row=1,column=0,columnspan=10)

    WidgetNames = ['Button', 'Canvas']
    for widget in WidgetNames:
        Listbox.insert(0, widget)
    myList.grid(row=0,column=0,columnspan=10)

def main():
    ListWindow()

if __name__ == "__main__":
    main()

推荐答案

您需要从小部件的 instance 调用方法.现在,您正在尝试使用Type作为实例.

You need to call method from instance of a widget. Right now you are trying to use Type as instance.

for widget in WidgetNames:
    myList.insert(0, widget) 

此外,不确定使用的是哪个IDE,但是即使某些IDE隐式调用mainloop,还是最好将其显式添加.

Also, not sure which IDE you are using but even if some IDEs calls mainloop implicitly, it would be better to add it explicitly.

def ListWindow():
    Listwindow = Tk() 
    ....
    ....
    myList.grid(row=0,column=0,columnspan=10)
    Listwindow.mainloop()

这篇关于tkinter列表框错误-AttributeError:"int"对象没有属性"tk"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆