在另一个模块中调用类函数

时间:2013-09-04 11:35:09

标签: python tkinter

我在gui中更新文本框时遇到问题。代码原则上如下所示:

    # The Static_textbox module
    # -------------------------

    class TP_textbox(tk.LabelFrame):


        def __init__(self, master = None,bg='#000000'):

            tk.LabelFrame.__init__(self, master,text = 'Information')

            self.grid(row = 0, column = 0, columnspan = 1,rowspan = 1, 
                      padx = 15, pady = 15,sticky = tk.N+tk.W + tk.S+tk.E)

            self.create()


        def create(self):    
            self.v = tk.StringVar()
            self.v.set('Choose time interval and press the "Read data" button to load data')
            self.textbox = tk.Label(self, textvariable = self.v, 
                                    anchor=tk.W, justify=tk.LEFT, font=("Arial", 10, 'bold'))
            self.textbox.grid()        
            print 'not again!!!!!!!!!!!!!!!!!!!!!!!!!!!'


        def update_textbox(self,TEXT):

            self.v.set(TEXT)
            self.update_idletasks()         




    # Module calling Static_textbox
    # -----------------------------

    import Static_textbox

    def createPickel(my_dir = u'/test_data', 
                     start = ['2012','1','2','12','45'],
                     stop = ['2012','6','2','12','45'],
                     filterVal = "filtered",
                     myData = transpaper_DataHolder.DataHolder()):

        update_info = Static_textbox.TP_textbox()

        infoText = 'Locating data on server...'
        update_info.update_textbox(infoText)

我意识到“update_info = Static_textbox.TP_textbox()”行执行整个模块,包括create函数,因此在旧的顶部创建一个新的文本框。显然我想只调用static_text模块中的更新函数,但这是如何完成的?我是这个面向对象编程的新手......对不起愚蠢的问题。

编辑:

在阅读下面的答案和评论后,我认为解决方案应该类似于以下内容

在主模块中创建文本框对象,并将其发送到“数据持有者”模块:

# Main module

import Static_textbox
import transpaper_DataHolder

    def __init__(self, root):
    """ """
        self.Data = transpaper_DataHolder.DataHolder() #class that holds data
        tk.Frame.__init__(self, root)
        root.title(""" FerryBox Tools """)
        self._setupMainWindow()

    def _setupMainWindow(self):
        TextObj = Static_textbox.TP_textbox(root)
        self.Data.setTextObj(TextObj)

数据持有者模块,您可以从中调用文本框对象中的“update_textbox”函数:

# Data holder module

class DataHolder(object):

        def setTextObj(self,TextObj):
            self.TextObj = TextObj

        def getTextObj(self):
            return self.TextObj

然后将从几个不同的模块调用“update_textbox”:

# Typical call from other modules       

import transpaper_DataHolder

def createPickel(my_dir = u'/test_data', 
         start = ['2012','1','2','12','45'],
         stop = ['2012','6','2','12','45'],
         filterVal = "filtered",
         myData = transpaper_DataHolder.DataHolder()):  

    Data = myData

    infoText = 'Locating data on server...'
    TextObj = Data.getTextObj()
    TextObj.update_textbox(infoText)    

但这不起作用..没有错误,但文本框未更新。任何sugestions?

1 个答案:

答案 0 :(得分:2)

您不能将update_textbook视为静态函数,因为它未声明为静态函数(其功能意味着它接收对象)。

在python中,self参数引用类的实例。因此,您只能在这种情况下使用它,如下所示:

# create a new text-book
textbook = Static_textbox.TP_textbox()

# update the textbook with some text
textbook.update_textbox("123")