当用户在其他窗口上单击“确定”时,我无法尝试刷新静态文本。当用户从不同的窗口添加内容时,我需要刷新主窗口。代码:
import wx
import random
class oranges(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Testing Sample',size=(1024,768))
self.frame=wx.Panel(self)
self.static_text=wx.StaticText(self.frame,-1,str(random.randint(1,100)),pos=(500,500))
re_button=wx.Button(self.frame,label='Refresh',pos=(200,200),size=(50,50))
self.Bind(wx.EVT_BUTTON,self.refresh,re_button)
re_buttoned=wx.Button(self.frame,label='ok',pos=(100,100),size=(50,50))
self.Bind(wx.EVT_BUTTON,self.another_thing,re_buttoned)
def refresh(self,event):
self.static_text.Destroy()
ra_again=random.randint(1,100)
self.static_text=wx.StaticText(self.frame,-1,str(ra_again),pos=(500,500))
def another_thing(self,event):
class apples(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Testing Different sample',size=(1024,768))
self.frames=wx.Panel(self)
self.static_text.Destroy()
ra_again=random.randint(1,100)
self.static_text=wx.StaticText(self.frame,-1,str(ra_again),pos=(500,500))
if __name__ =='__main__':
apps = wx.PySimpleApp()
windows = apples(parent=None,id=-1)
windows.Show()
apps.MainLoop()
if __name__ =='__main__':
app = wx.PySimpleApp()
window = oranges(parent=None,id=-1)
window.Show()
app.MainLoop()
按下按钮时出现此错误:AttributeError:'apples' object has no attribute 'static_text'
。对不起凌乱的代码,因为这只是为了表明问题所在。提前谢谢!期待着答案!
答案 0 :(得分:1)
你可以做到
def another_thing(self,event):
class apples(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Testing Different sample',size=(1024,768))
self.frames=wx.Panel(self)
window.static_text.Destroy()
ra_again=random.randint(1,100)
因为你没有在该函数中分配窗口python发现它在本地范围外部并查找全局命名空间匹配(可能是非本地/非全局命名空间?)
所有这一切都说我很难用这个好的用例...你应该尽量避免使用这种类型的代码而不是破解它...