下面是我正在处理的代码:
import wx
import wx.aui
import wx.lib.scrolledpanel
from parse import *
import settings
parser=parse()
ID_OPEN = 101
ID_SAVE = 102
ID_QUIT = 103
factors = ['Power','Fuel','Pot Water','Bottled Water','Storage Area','Personnel','Grey Water','Black Water','Solid Waste','Food','Material','Cost']
class TabPanel100(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
chpanel1=wx.Panel(self,-1)
static1 = wx.StaticText(chpanel1,wx.NewId(), "Power:",pos=(10,10))
self.text1 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[0]),size=(80,-1),pos=(140,10),style = wx.TE_PROCESS_ENTER,name = factors[0])
static2 = wx.StaticText(chpanel1,wx.NewId(),"Fuel:",pos=(10,35))
self.text2 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[1]),size=(80,-1),pos=(140,35),style = wx.TE_PROCESS_ENTER,name = factors[1])
static3 = wx.StaticText(chpanel1,wx.NewId(),"Pot Water:",pos=(10,60))
self.text3 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[2]),size=(80,-1),pos=(140,60),style = wx.TE_PROCESS_ENTER,name = factors[2])
static4 = wx.StaticText(chpanel1,wx.NewId(),"Bottled Water:",pos=(10,85))
self.text4 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[3]),size=(80,-1),pos=(140,85),style = wx.TE_PROCESS_ENTER,name = factors[3])
static5 = wx.StaticText(chpanel1,wx.NewId(),"Storage Area:",pos=(10,110))
self.text5 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[4]),size=(80,-1),pos=(140,110),style = wx.TE_PROCESS_ENTER,name = factors[4])
static6 = wx.StaticText(chpanel1,wx.NewId(),"Personnel:",pos=(10,135))
self.text6 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[5]),size=(80,-1),pos=(140,135),style = wx.TE_PROCESS_ENTER,name = factors[5])
static7 = wx.StaticText(chpanel1,wx.NewId(),"Grey Water:",pos=(10,160))
self.text7 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[6]),size=(80,-1),pos=(140,160),style = wx.TE_PROCESS_ENTER,name = factors[6])
static8 = wx.StaticText(chpanel1,wx.NewId(),"Black Water:",pos=(10,185))
self.text8 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[7]),size=(80,-1),pos=(140,185),style = wx.TE_PROCESS_ENTER,name = factors[7])
static9 = wx.StaticText(chpanel1,wx.NewId(),"Solid Waste:",pos=(10,210))
self.text9 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[8]),size=(80,-1),pos=(140,210),style = wx.TE_PROCESS_ENTER,name = factors[8])
static10 = wx.StaticText(chpanel1,wx.NewId(),"Food:",pos=(10,235))
self.text10 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.values[9]),size=(80,-1),pos=(140,235),style = wx.TE_PROCESS_ENTER,name = factors[9])
static11 = wx.StaticText(chpanel1,wx.NewId(),"Materials:",pos=(10,260))
self.text11 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.values[10]),size=(80,-1),pos=(140,260),style = wx.TE_PROCESS_ENTER,name = factors[10])
static12 = wx.StaticText(chpanel1,wx.NewId(),"Cost($k):",pos=(10,285))
self.text12 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.values[11]),size=(80,-1),pos=(140,285),style = wx.TE_PROCESS_ENTER,name = factors[11])
inputs = [self.text1,self.text2,self.text3,self.text4,self.text5,self.text5,self.text6,self.text7,self.text8,self.text9,self.text10,self.text11,self.text12]
for item in inputs:
item.Bind(wx.EVT_TEXT_ENTER, self.UpdateEstValues)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(chpanel1)
self.SetSizer(sizer)
def UpdateEstValues(self,event):
item = event.GetEventObject()
if float(item.GetValue()) < 0:
dlg = wx.MessageDialog(self, "Value cannot be negative.",'Error', wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
pass
else:
varName = item.GetName()
if varName == factors[0]:
parser.values[0] = float(item.GetValue())
class TabPanel200(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
chpanel1=wx.Panel(self,-1)
static1 = wx.StaticText(chpanel1,wx.NewId(),"Power:",pos=(10,10))
text1 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[0]),size=(80,-1),pos=(140,10))
static2 = wx.StaticText(chpanel1,wx.NewId(),"Fuel:",pos=(10,35))
text2 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[1]),size=(80,-1),pos=(140,35))
static3 = wx.StaticText(chpanel1,wx.NewId(),"Pot Water:",pos=(10,60))
text3 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[2]),size=(80,-1),pos=(140,60))
static4 = wx.StaticText(chpanel1,wx.NewId(),"Bottled Water:",pos=(10,85))
text4 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[3]),size=(80,-1),pos=(140,85))
static5 = wx.StaticText(chpanel1,wx.NewId(),"Storage Area:",pos=(10,110))
text5 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[4]),size=(80,-1),pos=(140,110))
static6 = wx.StaticText(chpanel1,wx.NewId(),"Personnel:",pos=(10,135))
text6 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[5]),size=(80,-1),pos=(140,135))
static7 = wx.StaticText(chpanel1,wx.NewId(),"Grey Water:",pos=(10,160))
text7 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[6]),size=(80,-1),pos=(140,160))
static8 = wx.StaticText(chpanel1,wx.NewId(),"Black Water:",pos=(10,185))
text8 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[7]),size=(80,-1),pos=(140,185))
static9 = wx.StaticText(chpanel1,wx.NewId(),"Solid Waste:",pos=(10,210))
text9 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[8]),size=(80,-1),pos=(140,210))
static10 = wx.StaticText(chpanel1,wx.NewId(),"Food:",pos=(10,235))
text10 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[9]),size=(80,-1),pos=(140,235))
static11 = wx.StaticText(chpanel1,wx.NewId(),"Materials:",pos=(10,260))
text11 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[10]),size=(80,-1),pos=(140,260))
static12 = wx.StaticText(chpanel1,wx.NewId(),"Cost($k):",pos=(10,285))
text12 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.totals[11]),size=(80,-1),pos=(140,285))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(chpanel1)
self.SetSizer(sizer)
class TabPanel300(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
chpanel1=wx.Panel(self,-1)
static1 = wx.StaticText(chpanel1,wx.NewId(),"Power:",pos=(10,10))
self.text1 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[0]),size=(80,-1),pos=(140,10))
static2 = wx.StaticText(chpanel1,wx.NewId(),"Fuel:",pos=(10,35))
text2 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[1]),size=(80,-1),pos=(140,35))
static3 = wx.StaticText(chpanel1,wx.NewId(),"Pot Water:",pos=(10,60))
text3 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[2]),size=(80,-1),pos=(140,60))
static4 = wx.StaticText(chpanel1,wx.NewId(),"Bottled Water:",pos=(10,85))
text4 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[3]),size=(80,-1),pos=(140,85))
static5 = wx.StaticText(chpanel1,wx.NewId(),"Storage Area:",pos=(10,110))
text5 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[4]),size=(80,-1),pos=(140,110))
static6 = wx.StaticText(chpanel1,wx.NewId(),"Personnel:",pos=(10,135))
text6 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[5]),size=(80,-1),pos=(140,135))
static7 = wx.StaticText(chpanel1,wx.NewId(),"Grey Water:",pos=(10,160))
text7 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[6]),size=(80,-1),pos=(140,160))
static8 = wx.StaticText(chpanel1,wx.NewId(),"Black Water:",pos=(10,185))
text8 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[7]),size=(80,-1),pos=(140,185))
static9 = wx.StaticText(chpanel1,wx.NewId(),"Solid Waste:",pos=(10,210))
text9 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[8]),size=(80,-1),pos=(140,210))
static10 = wx.StaticText(chpanel1,wx.NewId(),"Food:",pos=(10,235))
text10 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[9]),size=(80,-1),pos=(140,235))
static11 = wx.StaticText(chpanel1,wx.NewId(),"Materials:",pos=(10,260))
text11 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[10]),size=(80,-1),pos=(140,260))
static12 = wx.StaticText(chpanel1,wx.NewId(),"Cost($k):",pos=(10,285))
text12 = wx.TextCtrl(chpanel1,wx.NewId(),str(parser.factors[11]),size=(80,-1),pos=(140,285))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(chpanel1)
self.SetSizer(sizer)
class TabPanel1(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sizer)
class TabPanel2(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
chpanel2=wx.Panel(self,-1)
static11 = wx.StaticText(chpanel2,wx.NewId(),"Power:",pos=(10,10))
text11 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresources1),size=(80,-1),pos=(140,10))
static21 = wx.StaticText(chpanel2,wx.NewId(),"Fuel:",pos=(10,35))
text21 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces2),size=(80,-1),pos=(140,35))
static31 = wx.StaticText(chpanel2,wx.NewId(),"Pot Water:",pos=(10,60))
text31 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces3),size=(80,-1),pos=(140,60))
static41 = wx.StaticText(chpanel2,wx.NewId(),"Bottled Water:",pos=(10,85))
text41 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces4),size=(80,-1),pos=(140,85))
static51 = wx.StaticText(chpanel2,wx.NewId(),"Storage Area:",pos=(10,110))
text51 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces5),size=(80,-1),pos=(140,110))
static61 = wx.StaticText(chpanel2,wx.NewId(),"Personnel:",pos=(10,135))
text61 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces6),size=(80,-1),pos=(140,135))
static71 = wx.StaticText(chpanel2,wx.NewId(),"Grey Water:",pos=(10,160))
text71 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces7),size=(80,-1),pos=(140,160))
static81 = wx.StaticText(chpanel2,wx.NewId(),"Black Water:",pos=(10,185))
text81 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces8),size=(80,-1),pos=(140,185))
static91 = wx.StaticText(chpanel2,wx.NewId(),"Solid Waste:",pos=(10,210))
text91 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces9),size=(80,-1),pos=(140,210))
static101 = wx.StaticText(chpanel2,wx.NewId(),"Food:",pos=(10,235))
text101 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces10),size=(80,-1),pos=(140,235))
static111 = wx.StaticText(chpanel2,wx.NewId(),"Materials:",pos=(10,260))
text111 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces11),size=(80,-1),pos=(140,260))
static121 = wx.StaticText(chpanel2,wx.NewId(),"Cost($k):",pos=(10,285))
text121 = wx.TextCtrl(chpanel2,wx.NewId(),str(parser.totalresouces12),size=(80,-1),pos=(140,285))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(chpanel2)
self.SetSizer(sizer)
class ChoicebookDemo1(wx.Choicebook):
def __init__(self, parent):
wx.Choicebook.__init__(self, parent, wx.ID_ANY)
# Create the first tab and add it to the notebook
tabOne = TabPanel1(self)
tabOne.SetBackgroundColour("Gray")
self.AddPage(tabOne, "Select facility")
# Create and add the second tab
tabTwo = TabPanel100(self)
self.AddPage(tabTwo, "Electrical Distribution")
# Create and add the third tab
self.AddPage(TabPanel100(self), "Electrical Generation")
self.AddPage(TabPanel100(self), "Natural gas Distribution")
self.AddPage(TabPanel100(self), "Water distribution")
self.Bind(wx.EVT_CHOICEBOOK_PAGE_CHANGED, self.OnPageChanged)
def OnPageChanged(self, event):
old = event.GetOldSelection()
new = event.GetSelection()
page=self.GetPageText(event.GetSelection())
# sel = self.GetSelection()
print(page)
print 'OnPageChanged, old:%d, new:%dn' % (old, new)
event.Skip()
class ChoicebookDemo2(wx.Choicebook):
def __init__(self, parent):
wx.Choicebook.__init__(self, parent, wx.ID_ANY)
# Create the first tab and add it to the notebook
tabOne = TabPanel1(self)
tabOne.SetBackgroundColour("Gray")
self.AddPage(tabOne, "Select facility")
# Create and add the second tab
tabTwo = TabPanel200(self)
self.AddPage(tabTwo, "Values")
# Create and add the third tab
self.Bind(wx.EVT_CHOICEBOOK_PAGE_CHANGED, self.OnPageChanged)
def OnPageChanged(self, event):
old = event.GetOldSelection()
new = event.GetSelection()
# sel = self.GetSelection()
print 'OnPageChanged, old:%d, new:%dn' % (old, new)
event.Skip()
class ChoicebookDemo3(wx.Choicebook):
def __init__(self, parent):
wx.Choicebook.__init__(self, parent, wx.ID_ANY)
# Create the first tab and add it to the notebook
tabOne = TabPanel1(self)
tabOne.SetBackgroundColour("Gray")
self.AddPage(tabOne, "Select facility")
# Create and add the second tab
tabTwo = TabPanel300(self)
self.AddPage(tabTwo, "Electrical Distribution")
# Create and add the third tab
self.AddPage(TabPanel300(self), "Electrical Generation")
self.AddPage(TabPanel300(self), "Natural gas Distribution")
self.AddPage(TabPanel300(self), "Water distribution")
self.Bind(wx.EVT_CHOICEBOOK_PAGE_CHANGED, self.OnPageChanged)
def OnPageChanged(self, event):
old = event.GetOldSelection()
new = event.GetSelection()
# sel = self.GetSelection()
print 'OnPageChanged, old:%d, new:%dn' % (old, new)
event.Skip()
class Panel(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self,parent=parent)
nb1=wx.aui.AuiNotebook(self)
page1=[(TabPanel2(nb1),"Overall Resources"),(ChoicebookDemo1(nb1),"factors"),(ChoicebookDemo2(nb1),"values"),(ChoicebookDemo3(nb1),"totals")]
for page1, label1 in page1:
nb1.AddPage(page1,label1)
sizer=wx.BoxSizer(wx.VERTICAL)
sizer.Add(nb1,1,wx.EXPAND)
self.SetSizer(sizer)
class MyFrame(wx.Frame):
def __init__(self, parent, id=-1, title='wx.aui Test',
pos=wx.DefaultPosition, size=(1000, 500),
style=wx.DEFAULT_FRAME_STYLE):
wx.Frame.__init__(self, parent, id, title, pos, size, style)
menubar =wx.MenuBar()
file = wx.Menu()
edit = wx.Menu()
help = wx.Menu()
tools = wx.Menu()
pr = wx.Menu()
file.Append(ID_OPEN, 'Open...', 'Load a specific dataset')
file.Append(ID_SAVE, 'Save As...', 'Save current dataset')
file.Append(ID_QUIT, 'Quit', 'Quit application')
menubar.Append(file,'&File')
menubar.Append(edit,'&Edit')
menubar.Append(help,'&Help')
menubar.Append(tools,'&Tools')
menubar.Append(pr,'&PR')
self.SetMenuBar(menubar)
self._mgr = wx.aui.AuiManager(self)
self.Bind(wx.EVT_CLOSE, self.OnClose)
toppanel=wx.Panel(self)
text3 = Panel(self)
sizer=wx.BoxSizer(wx.VERTICAL)
sizer.Add(text3,0,wx.ALL,border=10)
toppanel.SetSizer(sizer)
# add the panes to the manager
self._mgr.AddPane(text3, wx.CENTER,'Restoration data')
# tell the manager to 'commit' all the changes just made
self._mgr.Update()
self.Bind(wx.EVT_CLOSE, self.OnClose)
def OnQuit(self,event):
self.Close()
def OnClose(self, event):
# deinitialize the frame manager
self._mgr.UnInit()
# delete the frame
self.Destroy()
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()
我是一个新手,开始为一个类项目使用wxpython。
主机有一本笔记本。Auinotebook是由Choicebook面板组成的。每个Choicebook面板由12个textctrl变量组成。
如果用户更改了一个选择簿面板中的值,我也需要更改其他选择簿面板中的相应值。
这些值在数学上是相关的。我无法更新其他面板的值。
任何关于这方面的帮助将是伟大的。
谢谢你花时间来帮我。您已经正确地开始将EVT_TEXT_ENTER绑定到UpdateEstValues。你得到了输入的值,你唯一需要做的就是把这个值转移到第二个选项卡中,对吧?您似乎也使用解析器作为"模型",保持当前有效的值对吗?因此,在UpdateEstValues中,您可以更新模型(解析器),您可能可以自己完成工作,您只需要告诉第二个选项卡更新其值。
从结构上讲,有很多方法可以做到这一点,但它都归结为某种触发事件,告诉第二个选项卡更新。所以最基本的想法是有一个函数解析器。Fire和parser.add_listener。第二个选项卡将有一个函数updatedef update(self, parameter_name):
self.text_edits[parameter_name].SetValue(parser.get_value("parameter_name"))
在创建制表符后的主框架中:
parser.add_listener(second_tab.update)
在你的UpdateEstValues你会做:
parser.fire(parameter_name)
We are missing:
def add_listener(self, listener):
self.listeners.append(listener)
def fire(self, parameter_name):
for i in self.listeners:
i(parameter_name)
当然是一种方法的最小演示。您可以查找pubsub包,也可以使用wx事件(用户定义的)通知第二个选项卡更改,但原则上它们都是相似的。