【问题标题】:python get items from def within a classpython从类中的def获取项目
【发布时间】: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


【解决方案1】:

根据您的评论,您想在__init__ 中使用您在myicon 中创建的base64 图像

问题是当你调用类时myicon还没有完成(它的__init__

我想最好的方法是在__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 窗口单独编写为一个类,然后在一个类中编写 mainloop 它。

【讨论】:

  • 我不知道如何将 def 调用到 init
  • 你的意思是 def myicon(self):
  • 在我的例子中被调用。 self.myicon(...) 但你需要 tp 传递一些东西,因为它需要在你的代码中添加一个额外的参数
  • 我发布了一个新问题。请看一看。谢谢stackoverflow.com/questions/36062041/…
猜你喜欢
  • 1970-01-01
  • 2012-05-23
  • 2014-06-20
  • 2022-10-04
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
  • 2012-08-09
  • 1970-01-01
相关资源
最近更新 更多