且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将图像添加到 tkinter gui

更新时间:2023-12-05 12:24:10

每次你想在一个类的方法中使用它的属性时,你需要在它的前面加上 self. 前缀:

Every time you want to use an attribute of a class inside one of its methods, you need to prefix it with self.:

self.item = self.can.create_image(80, 80, image=self.pic)
#           ^^^^^                               ^^^^^

否则,Python 会将名称视为函数的本地名称,并在找不到它们时引发异常.

Otherwise, Python will treat the names as being local to the function and will raise an exception when it fails to find them.

此外,您忘记在画布小部件上调用 grid:

Also, you forgot to call grid on your canvas widget:

self.can = Canvas(root, width=160, height=160, bg='white')
self.can.grid(...)

关于 Tkinter 的资源,您可以查看这些:


As for resources on Tkinter, you can check out these: