【发布时间】:2014-07-21 22:28:57
【问题描述】:
当我尝试向按钮添加图像时,程序将运行,但按钮将是空白的,您无法单击它。如果我将 image=Tkinter.PhotoImage(file="C:/TeDOC/OpenFolder.gif") 更改为 text='Open Directory 它工作正常,您可以单击该按钮。我不知道为什么当我将其更改为 img 时,它不起作用。任何帮助将不胜感激。
这是我的代码:
import Tkinter, Tkconstants, tkFileDialog
class TkFileDialogExample(Tkinter.Frame):
def __init__(self, root):
Tkinter.Frame.__init__(self, root)
# options for buttons
button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
# define buttons
Tkinter.Button(self, image=Tkinter.PhotoImage(file="C:/TeDOC/OpenFolder.gif"), command=self.askdirectory).pack(**button_opt)
# defining options for opening a directory
self.dir_opt = options = {}
options['initialdir'] = 'C:\\'
options['mustexist'] = False
options['parent'] = root
options['title'] = 'This is a title'
def askdirectory(self):
#Returns a selected directoryname.
return tkFileDialog.askdirectory(**self.dir_opt)
if __name__=='__main__':
root = Tkinter.Tk()
TkFileDialogExample(root).pack()
root.mainloop()
【问题讨论】:
标签: python image python-2.7 tkinter photoimage