简体   繁体   English

为什么我不能对位图图像或照片图像tkinter进行网格划分?

[英]Why can't I grid a bitmap image or photo image tkinter?

I have this code, which uses the tkinter bitmap image: 我有这个代码,它使用tkinter位图图像:

def ask(msg='Question?',title='Question'):
    root=Tk()
    thing=Thing()
    root.title(title)
    imgw=BitmapImage(root,file='Question.xbm')
    root.iconbitmap('Question.ico')
    imgw.grid(rowspan=2,padx=5,pady=5,sticky=NSEW)
    msgw=Label(root,text=msg)
    msgw.grid(column=1,padx=10,pady=5,sticky=NSEW,columnspan=2)
    button1=Button(root,text='Yes',command=lambda:thing.change('Yes',root),underline=0)
    button1.grid(column=1,row=1,padx=5,pady=5,sticky=NSEW)
    button1.focus_set()
    button2=Button(root,text='No',command=lambda:thing.change('No',root),underline=0)
    button2.grid(column=2,row=1,padx=5,pady=5,sticky=NSEW)
    root.bind('Key-y',lambda:thing.change('Yes',root))
    root.bind('Key-n',lambda:thing.change('No',root))
    root.mainloop()
ask()

...but I can't grid the bitmap image. ...但我无法对位图图像进行网格化。 I've tried poth photo image and bitmap image but they both say: 我试过poth照片图像和位图图像,但他们都说:

Traceback (most recent call last):
File "E:\gui.py", line 18, in <module>
ask()
File "E:\gui.py", line 7, in ask
imgw.grid(rowspan=2,padx=5,pady=5,sticky=NSEW)
AttributeError: 'BitmapImage' object has no attribute 'grid'

I am using Python 3.4.2. 我使用的是Python 3.4.2。 Is there a way of doing this or is it just an annoying thing in tkinter? 有没有办法做到这一点,还是在tkinter中只是一件烦人的事情?

By the way, here is the Thing class: 顺便说一下,这是Thing类:

class Thing:
    def __init__(self,val=None):
        self.val=val
    def change(self,val=None,win=None):
        self.val=val
        if win:win.destroy()

A BitmapImage is not a widget, like a Label or a Button . BitmapImage不是小部件,如LabelButton It can not be added directly to root and laid out with grid . 它不能直接添加到root并使用grid布局。 Instead, you have to add it to, eg, another Label (or the same Label you use for the question, if you prefer). 相反,您必须将其添加到例如另一个Label (或者您用于问题的相同Label ,如果您愿意)。

root=Tk()

imgw = BitmapImage(file='Question.xbm')            # no 'root' parameter
imgLabel = Label(root,image=imgw)                  # wrap the BitmapImage
imgLabel.grid(rowspan=2,padx=5,pady=5,sticky=NSEW) # layout the label

msgw=Label(root,text="Question")
msgw.grid(column=1,padx=10,pady=5,sticky=NSEW,columnspan=2)
button1=Button(root,text='Yes')
button1.grid(column=1,row=1,padx=5,pady=5,sticky=NSEW)
button2=Button(root,text='No')
button2.grid(column=2,row=1,padx=5,pady=5,sticky=NSEW)
root.mainloop()

Also note that images like this tend to be garbage collected even when used in a Label. 另请注意,即使在Label中使用,这样的图像也会被垃圾收集。 To prevent this, you should make the BitmapImage a global variable, or put it into a global container, eg a dict mapping filenames to already loaded images. 为了防止这种情况,你应该做BitmapImage一个全局变量,或者把它变成一个全球性的容器,例如dict映射文件名已经加载的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM