我有这个代码,它使用 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()
Run Code Online (Sandbox Code Playgroud)
...但我无法对位图图像进行网格化。我试过 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'
Run Code Online (Sandbox Code Playgroud)
我正在使用 Python 3.4.2。有没有办法做到这一点,或者它只是 tkinter 中的一件烦人的事情?
顺便说一下,这是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()
Run Code Online (Sandbox Code Playgroud)