问题描述
使用Tkinter 8.6,Python 3.7.3:
这里一个友好的用户通过创建 Imgbutton
类(它是Tkinter Canvas
的子类)的方式指导我如何使图像像按钮一样./p>
我对此代码有一些疑问,这是它的简化版本:
#!/usr/local/bin/python3将tkinter导入为tk从PIL导入Image,ImageTkImgbutton(tk.Canvas)类:def __init __(self,master = None,image = None,command = None,** kw):超级(Imgbutton,self).__ init __(master = master,** kw)
self.set_img = self.create_image(0,0,锚='nw',image = image)
self.bind_class(self,'< Button-1>',lambda _:self.config(relief ='sunken'),add ="+")self.bind_class(self,'< ButtonRelease-1>',lambda _:self.config(relief ='groove'),add ='+')self.bind_class(self,'< Button-1>',lambda _:command()如果命令为else,则无,添加="+")
问题:
- 当我创建一个
Imgbutton
对象时,上面的分隔行被执行,但是我不明白为什么. -
self.set_img
是否对应于Imgbutton
或tk.Canvas
类的对象? - 在这里是否有任何地方可以创建实际的画布?我相信您需要先创建一个画布,然后才能向其中添加任何内容.
这部分可能不需要提及,但是在这里我要创建一个 Imgbutton
对象:
root = tk.Tk()but_img = tk.PhotoImage(file ='button.png')但是= Imgbutton(root,image = but_img,width = but_img.width(),height = but_img.height(),borderwidth = 2,highlightthickness = 0)但是.pack()root.mainloop()
它被执行,因为它是代码的一部分.我不确定为什么您会认为它不会被调用.如果不想调用它,请将其移到 __ init __
方法之外.
self
是指 Imgbutton
类的实例. set_img
是画布在画布上创建对象时返回的标识符.
是的. Imgbutton
是画布.这就是继承的工作方式: Imgbutton
是 Canvas
,具有一些增强功能.它是在您执行 but = Imgbutton(...)
时创建的.但是,当您调用 super
时,可能会更准确地创建实际的画布,这告诉tkinter创建对象.
Using Tkinter 8.6, Python 3.7.3:
A friendly user here instructed me on how to have an image act like a button by the way of creating an Imgbutton
class that is a subclass of Tkinter Canvas
.
I have some questions regarding this code, here is the simplified version of it:
#!/usr/local/bin/python3
import tkinter as tk
from PIL import Image, ImageTk
class Imgbutton(tk.Canvas):
def __init__(self, master=None, image=None, command=None, **kw):
super(Imgbutton, self).__init__(master=master, **kw)
self.set_img = self.create_image(0, 0, anchor='nw', image=image)
self.bind_class( self, '<Button-1>',
lambda _: self.config(relief='sunken'), add="+")
self.bind_class( self, '<ButtonRelease-1>',
lambda _: self.config(relief='groove'), add='+')
self.bind_class( self, '<Button-1>',
lambda _: command() if command else None, add="+")
Questions:
- When I create an
Imgbutton
object, the separated line above gets executed but I do not understand why. - Does the
self.set_img
correspond to an object ofImgbutton
ortk.Canvas
class? - Is there any point here where an actual canvas is created? I believed you need to create a canvas before you can add anything to it.
This part might be unneccessary to mention but here I am creating an Imgbutton
object:
root = tk.Tk()
but_img = tk.PhotoImage(file='button.png')
but = Imgbutton(root, image=but_img, width=but_img.width(),
height=but_img.height(), borderwidth=2, highlightthickness=0)
but.pack()
root.mainloop()
It's executed because it's part of the code. I'm not sure why you think it wouldn't be called. If you don't want it to be called, move it outside of the __init__
method.
self
refers to the instance of the Imgbutton
class. set_img
will be the identifier returned by the canvas when it creates the object on the canvas.
Yes. Imgbutton
is the canvas. That is how inheritance works: Imgbutton
is a Canvas
, with some enhancements. It gets created when you do but = Imgbutton(...)
. Though, perhaps a bit more accurately the actual canvas is created when you call super
, which tells tkinter to create the object.
这篇关于Python Tkinter画布类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!