0

I want to get the base64 image from the def and use them in main init def. So far I have only been able to get one. I'm new to using class and things seem to work differently.

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
                """
k4ppa
  • 4,122
  • 8
  • 26
  • 36
  • I really don't understand what you are trying to do, could you elaborate on it? – Bernardo Meurer Mar 09 '16 at 14:53
  • Within deff myicon I have two base64 images and I want to use them in my Tkinter window. however I am failing miserably. I tried putting them in a dictionary {"drive":drive, "appicon":appicon} but when I try to use them in the def __init__ it gives me an error. – John Shiveley Mar 09 '16 at 15:07
  • which error does it yield? – Bernardo Meurer Mar 09 '16 at 15:09
  • TypeError: 'instancemethod' object has no attribute '__getitem__' the above code does not show the dictionary because I tried another method. I can repost if needed – John Shiveley Mar 09 '16 at 15:13
  • I don't know how to call a def into init – John Shiveley Mar 09 '16 at 15:29
  • Bernard I have another question I think you would be equipped to answer. http://stackoverflow.com/questions/36038731/dictionary-items-not-working-when-passed-through-powershell-command Thanks – John Shiveley Mar 16 '16 at 16:11

1 Answers1

0

According to your to your comment, you want to use your base64 images created in myicon insite the __init__

The problem is that myicon is not yet done when you call the class (its __init__)

I guess the best way is either to create the images inside __init__ or to just call the myicon definition inside __init__ and add the base64 images to self. This way you ll be able to use them.

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
                """

It is not clear for me what you want to achieve, but i think it would be better to just write the Tkinter window as a class by itself, then to mainloop it inside a class.

Gábor Erdős
  • 3,599
  • 4
  • 24
  • 56