【发布时间】:2016-06-24 00:10:31
【问题描述】:
我想从 def 中获取 base64 图像并在 main init def 中使用它们。到目前为止,我只能得到一个。我是使用类的新手,事情似乎有所不同。
from Tkinter import Tk, Radiobutton, Button
import os, base64
class Select:
def __init__ (self, root):
self.root = root
root.title ('John Shiveley CheckScan Solution')
self.items = {}
appicon = self.myicons
##The Base64 icon version as a string
icon = appicon()
icondata= base64.b64decode(icon)
## The temp file is icon.ico
tempFile= "icon.ico"
iconfile= open(tempFile,"wb")
## Extract the icon
iconfile.write(icondata)
iconfile.close()
root.wm_iconbitmap(tempFile)
os.remove(tempFile)
button1 = Radiobutton (self.root, text = 'Option 1', command = self.option1, value = 1)
button1.grid(row=1,column=0,sticky="nw")
button2 = Radiobutton (self.root, text = 'Option 2', command = self.option2, value = 2)
button2.grid(row=1,column=1,sticky="ne")
b1 = Button (self.root, text = 'Run',width=20, command = self.test)
b1.grid(row=3,column=0, columnspan=2)
self.flag = 0
self.root.mainloop()
def option1 (self):
print ' Option 1'
self.flag = 1
def option2 (self):
print ' Option 2'
self.flag = 2
def test (self):
if self.flag == 1:
print '1st option selected'
elif self.flag == 2:
print '2nd option selected'
else:
print 'No option selected'
def myicons(appicon):
drive = \
"""
Image code
"""
appicon = \
"""
Image code
"""
【问题讨论】:
-
我真的不明白你在做什么,你能详细说明一下吗?
-
在 def myicon 我有两个 base64 图像,我想在我的 Tkinter 窗口中使用它们。但是我失败得很惨。我尝试将它们放入字典 {"drive":drive, "appicon":appicon} 但是当我尝试在 def init 中使用它们时,它给了我一个错误。
-
它会产生哪个错误?
-
TypeError: 'instancemethod' object has no attribute 'getitem' 上面的代码没有显示字典,因为我尝试了另一种方法。如果需要,我可以重新发布
-
我不知道如何将 def 调用到 init 中
标签: python class dictionary return base64