python 从 class 中的 def 获取项目
python get items from def within a class
我想从 def 中获取 base64 图像并在 main init def 中使用它们。到目前为止,我只能得到一个。我刚开始使用 class,但使用起来似乎有所不同。
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
"""
根据您的评论,您希望在 __init__
中使用在 myicon
中创建的 base64 图像
问题是当您调用 class(其 __init__
)
时 myicon
尚未完成
我想最好的方法是在 __init__
中创建图像,或者只调用 __init__
中的 myicon
定义并将 base64 图像添加到 self
.这样你就可以使用它们了。
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 () was missing, and you must pass an argument as well (appicon)
##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(self,appicon):
self.drive = \
"""
Image code
"""
self.appicon = \
"""
Image code
"""
我不清楚你想要实现什么,但我认为最好将 Tkinter
window 单独写成 class,然后mainloop
里面有一个class.
我想从 def 中获取 base64 图像并在 main init def 中使用它们。到目前为止,我只能得到一个。我刚开始使用 class,但使用起来似乎有所不同。
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
"""
根据您的评论,您希望在 __init__
myicon
中创建的 base64 图像
问题是当您调用 class(其 __init__
)
myicon
尚未完成
我想最好的方法是在 __init__
中创建图像,或者只调用 __init__
中的 myicon
定义并将 base64 图像添加到 self
.这样你就可以使用它们了。
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 () was missing, and you must pass an argument as well (appicon)
##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(self,appicon):
self.drive = \
"""
Image code
"""
self.appicon = \
"""
Image code
"""
我不清楚你想要实现什么,但我认为最好将 Tkinter
window 单独写成 class,然后mainloop
里面有一个class.