python从类中获取def中的项目

时间:2016-03-09 14:40:11

标签: python class dictionary return base64

我想从def获取base64图像并在主 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
                """

1 个答案:

答案 0 :(得分:0)

根据您的评论,您希望使用myicon中创建的base64图片__init__

问题是,当您致电课程时,myicon尚未完成__init__

我想最好的方法是在__init__内创建图片,或者只调用myicon内的__init__定义,然后将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窗口单独编写为一个类,然后在类中mainloop它会更好。