更新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_文本框()”执行包括create函数在内的整个模块,从而在旧的文本框上创建一个新的文本框。显然,我只想调用static_text模块中的update函数,但这是如何实现的呢?我是这个面向对象编程的新手…对不起这个愚蠢的问题。在
编辑:
在阅读了下面的答案和评论之后,我认为解决方案应该是如下所示的
在主模块中创建textbox对象并将其发送到“数据保持器”模块:
^{pr2}$
一个数据保持器模块,从中可以调用textbox对象中的“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)
但是这不起作用..没有错误,但是文本框没有更新。有什么建议吗?在