我在我的 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()”这一行执行了包括创建函数在内的整个模块,从而在旧文本框之上创建了一个新文本框。显然我只想调用 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)
但是这不起作用..没有错误,但文本框没有更新。有什么建议吗?