如何解决从另一个类访问小部件?
我正在尝试从另一个类更新 wx.ListCtrl。这让我发疯,我不明白我到底做错了什么。小部件是在 wx.Frame 中创建的,如:
class DataTable(gridlib.GridTableBase):
def __init__(self,data):
gridlib.GridTableBase.__init__(self)
self.data = data
#Store the row and col length to see if table has changed in size
self._rows = self.GetNumberRows()
self._cols = self.GetNumberCols()
print(self._rows,self._cols)
self.odd=gridlib.GridCellAttr()
self.odd.SetBackgroundColour((217,217,217))
self.even=gridlib.GridCellAttr()
self.even.SetBackgroundColour((255,255,255))
def GetAttr(self,row,col,kind):
attr = [self.even,self.odd][row % 2]
attr.IncRef()
return attr
def GetNumberRows(self):
return len(self.data)
def GetNumberCols(self):
return len(self.data.columns) + 1
def IsEmptyCell(self,col):
return False
def GetValue(self,col):
if col == 0:
pass
# return None #self.data.index[row]
else:
return self.data.iloc[row,col-1]
def SetValue(self,value):
if col == 0:
pass
else:
self.data.iloc[row,col - 1] = value
def GetColLabelValue(self,col):
try:
if col == 0:
return ""
#return 'Index' if self.data.index.name is None else self.data.index.name
else:
return self.data.columns[col - 1] #[col-1]
except:
pass
class DataGrid(gridlib.Grid):
def __init__(self,parent,data): # data
gridlib.Grid.__init__(self,- 1) #,colnames,-1 # data
self.table = DataTable(data)
self.SetTable(self.table,True)
self.Bind(wx.grid.EVT_GRID_CELL_CHANGED,self.onCellChanged)
def onCellChanged(self,event):
#Establish connection
self.connect_mysql()
#Update database
key_id = str("'") + self.GetCellValue(event.GetRow(),1) + str("'") #FOR TESTING. CHANGE TO 0 WHEN IMPORT IS FIXED
target_col = self.GetColLabelValue(event.GetCol())
key_col = self.GetColLabelValue(1) #FOR TESTING. CHANGE TO 0 WHEN IMPORT IS FIXED
nVal = str("'") + self.GetCellValue(event.GetRow(),event.GetCol()) + str("'")
sql_update = "UPDATE " + tbl + " SET " + target_col + " = " + nVal + " WHERE " + key_col + " = " + key_id + ""
try:
self.cursor.execute(sql_update)
stat = "UPDATE"
oVal = event.GetString()
action = "UPDATE"
msg = "Changed " + str("'") + oVal + str("'") + " to " + nVal + " for " + key_id + " in table: " + str("'") + tbl + str("'")
#self.updateStatus()
except:
stat = "ERROR"
msg = "ERROR: Failed to update SQL table. " + "'" + tbl + "'"
self.db_con.rollback()
#Update dataframe
#Log the change
#MainFrame.test_print(stat,msg)
#MainFrame.lc_change.Append(["1","2","3" + "\n"])
#Close connection
self.close_connection()
class MainFrame(wx.Frame):
def __init__(self,data): # (self,data):
wx.Frame.__init__(self,-1,"Varkey Foundation") #,size=(640,480))
#Create a panel
self.p = wx.Panel(self)
self.Maximize(True)
#Create blank dataframe
data = pd.DataFrame() #pd.DataFrame(np.random.randint(0,100,size=(200,5)),columns=list('EFGHD')
#data.reset_index(drop=True,inplace=True)
self.data = DataTable(data)
self.nb = wx.Notebook(self.p)
self.p.SetBackgroundColour( wx.Colour( 0,0 ) ) # 38,38,38
self.nb.SetBackgroundColour(wx.Colour(58,56,56) )
#self.SetBackgroundColour( wx.Colour( 255,56 ) )
#create the page windows as children of the notebook
self.page1 = PageOne(self.nb)
self.page2 = PageTwo(self.nb)
self.page3 = PageThree(self.nb)
# add the pages to the notebook with the label to show on the tab
self.nb.AddPage(self.page1,"Data")
self.nb.AddPage(self.page2,"Analyze")
self.nb.AddPage(self.page3,"Change Log")
#Create the grid and continue layout
self.grid = DataGrid(self.page1,data)
#grid.SetReadOnly(5,5,True)
#CreateFonts
self.b_font = wx.Font(14,wx.ROMAN,wx.NORMAL,wx.BOLD,True)
self.lbl_font = wx.Font(14,True)
self.cb_font = wx.Font(11,wx.SCRIPT,wx.ITALIC,True)
self.h_font = wx.Font(18,wx.DECORATIVE,True)
#Create Title bmp
ico = wx.Icon('varkey_bmp.bmp',wx.BITMAP_TYPE_ICO) #'varkey_frame.bmp'
self.SetIcon(ico)
#Page 1 sizers and widgets
self.title = wx.StaticText(self.page1,label="TITLE",style = wx.ALIGN_CENTER | wx.ST_NO_AUTORESIZE)
self.title.SetForegroundColour((255,255))
self.title.SetFont(self.h_font)
self.p1_sizer = wx.BoxSizer(wx.VERTICAL)
self.p1_sizer.Add(self.title,wx.EXPAND,5)
self.p1_sizer.Add(self.grid,3,wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM,25)
#self.p1_sizer.Add(self.btn_new,-0,wx.ALIGN_CENTER,5)
self.page1.SetSizer(self.p1_sizer)
#Page 2 sizers and widgets
self.analyze_grid = gridlib.Grid(self.page2)
self.analyze_grid.CreateGrid(0,10)
self.p2_sizer = wx.BoxSizer(wx.VERTICAL)
self.p2_sizer.Add(self.analyze_grid,1,wx.EXPAND)
self.page2.SetSizer(self.p2_sizer)
#Page 3 sizers and widgets
self.log_grid = gridlib.Grid(self.page3)
self.log_grid.CreateGrid(0,9)
self.log_grid.EnableEditing(False)
self.p3_sizer = wx.BoxSizer(wx.VERTICAL)
self.p3_sizer.Add(self.log_grid,wx.EXPAND)
self.page3.SetSizer(self.p3_sizer)
#Create widgets for top sizer
#Insert Image
self.staticbitmap = wx.StaticBitmap(self.p)
self.staticbitmap.SetBitmap(wx.Bitmap('varkey_logo2.jpg'))
self
self.lbl_user = wx.StaticText(self.p,label="Username:")
self.lbl_password = wx.StaticText(self.p,label="Password:")
self.lbl_interaction = wx.StaticText(self.p,label="Interaction:")
self.lbl_table = wx.StaticText(self.p,label="Table:")
#SetForground colors
self.lbl_user.SetForegroundColour((255,255))
self.lbl_password.SetForegroundColour((255,255))
self.lbl_interaction.SetForegroundColour((255,255))
self.lbl_table.SetForegroundColour((255,255))
#Set Fonts
self.lbl_user.SetFont(self.lbl_font)
self.lbl_password.SetFont(self.lbl_font)
self.lbl_interaction.SetFont(self.lbl_font)
self.lbl_table.SetFont(self.lbl_font)
self.tc_user =wx.TextCtrl(self.p,value='cmccall95',size = (130,25))
self.tc_password =wx.TextCtrl(self.p,value='Achilles95',style=wx.TE_PASSWORD | wx.TE_PROCESS_ENTER,25))
self.tc_password.Bind(wx.EVT_TEXT_ENTER,self.onLogin)
self.tc_user.SetFont(self.cb_font)
self.tc_password.SetFont(self.cb_font)
self.btn_login = wx.Button(self.p,label="Login",size=(105,30))
self.btn_login.SetBackgroundColour(wx.Colour(198,89,17))
self.btn_login.SetFont(self.b_font)
self.btn_login.Bind(wx.EVT_BUTTON,self.onLogin) #connect_mysql
self.btn_logout = wx.Button(self.p,label="Logout",30))
self.btn_logout.SetBackgroundColour(wx.Colour(192,0))
self.btn_logout.SetFont(self.b_font)
#self.btn_logout.Bind(wx.EVT_BUTTON,self.onLogout)
self.combo_interaction = wx.ComboBox(self.p,size = (160,25),style = wx.CB_READONLY | wx.CB_SORT | wx.CB_SORT)
self.combo_interaction.Bind(wx.EVT_COMBOBOX,self.onComboInteraction)
self.combo_table = wx.ComboBox(self.p,style = wx.CB_READONLY | wx.CB_SORT | wx.CB_SORT)
self.combo_table.Bind(wx.EVT_COMBOBOX,self.onHideCommands)
self.combo_interaction.SetFont(self.cb_font)
self.combo_table.SetFont(self.cb_font)
#self.combo_table.Bind(wx.EVT_COMBOBOX,self.OnComboTable)
self.btn_load = wx.Button(self.p,label="Load Table",30))
self.btn_load.SetBackgroundColour(wx.Colour(31,216,6))
self.btn_load.SetFont(self.b_font)
#self.btn_load.Bind(wx.EVT_BUTTON,self.onLoadData)
self.btn_load.Bind(wx.EVT_BUTTON,self.test_reload)
self.lc_change = wx.ListCtrl(self.p,style = wx.TE_MULTILINE | wx.LC_REPORT | wx.LC_VRULES)
self.lc_change.InsertColumn(0,"User ID")
self.lc_change.InsertColumn(1,"Status")
self.lc_change.InsertColumn(2,"Description")
self.lc_change.InsertColumn(3,"Date/Time")
#Set column widths
self.lc_change.SetColumnWidth(0,75)
self.lc_change.SetColumnWidth(1,75)
self.lc_change.SetColumnWidth(2,450)
self.lc_change.SetColumnWidth(3,125)
#Add Row Button
#self.btn_new = wx.Button(self.page1,label="+",size = (35,25))
#self.btn_new.SetForegroundColour(wx.Colour(112,173,71))
#self.btn_new.SetFont(self.h_font)
#self.btn_new.Bind(wx.EVT_BUTTON,self.onInsertRecordBelow)
#Create Filler text
self.lbl_filler = wx.StaticText(self.p,label="",size = (125,20))
#Create FlexGridSizers(For top half)
self.left_fgs = wx.FlexGridSizer(3,4,25,15)
self.left_fgs.AddMany([(self.lbl_user,wx.ALIGN_LEFT | wx.LEFT,15),(self.tc_user,wx.EXPAND),(self.lbl_interaction,wx.ALIGN_RIGHT|wx.RIGHT,10),(self.combo_interaction,(self.lbl_password,wx.ALIGN_LEFT| wx.LEFT,(self.tc_password,(self.lbl_table,(self.combo_table),(self.btn_login,2,(self.btn_logout,(self.lbl_filler,(self.btn_load,1)])
#Create Top Sizer
self.top_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.top_sizer.Add(self.left_fgs,proportion = 1,flag = wx.ALL|wx.EXPAND,border = 30)
self.top_sizer.Add(self.staticbitmap,wx.TOP | wx.RIGHT,border = 40) #30
self.top_sizer.Add(self.lc_change,wx.RIGHT|wx.EXPAND,30)
#create Bottom Sizer
self.bottom_sizer = wx.BoxSizer(wx.VERTICAL)
self.bottom_sizer.Add(self.nb,proportion = 5,flag = wx.LEFT |wx.RIGHT | wx.EXPAND,border = 30)
self.mainsizer = wx.BoxSizer(wx.VERTICAL)
self.mainsizer.Add(self.top_sizer,proportion = 0,border = 5)
self.mainsizer.Add(self.bottom_sizer,border = 5)
#self.mainsizer.Add(self.status_sizer,proportion =0,flag = wx.BOTTOM|wx.ALIGN_CENTER_HORIZONTAL,border = 15)
self.p.SetSizerAndFit(self.mainsizer)
self.hideWidgets()
self.onHideCommands(self)
#### Some functions...
if __name__ == '__main__':
import sys
app = wx.App()
frame = MainFrame(None,sys.stdout) # (None,sys.stdout)
frame.Show(True)
app.MainLoop()
我有另一个网格类,其中有一些绑定事件。
class DataGrid(gridlib.Grid):
def __init__(self,data):
gridlib.Grid.__init__(self,-1
self.table = DataTable(data)
self.SetTable(self.table,True)
self.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK,self.OnCellRightClick)
self.Bind(wx.grid.EVT_GRID_CELL_CHANGED,self.onCellChanged)
def onCellChanged(self,event):
try:
stat = "UPDATE"
msg = "Changed " + str("'") + self.oVal + str("'") + " to " + self.nVal + " for " + self.key_id + " in table: " + str("'") + tbl + str("'")
except:
stat = "ERROR"
msg = "ERROR: Failed to update SQL table. " + "'" + tbl + "'"
#self.updateStatus()
#MainFrame.test_print(stat,msg)
#self.MainFrame.lc_change.Append(["1","3" + "\n"])
我尝试将其直接传递给函数。
MainFrame.test_print(stat,msg)
哪个可以传递变量。问题是在使用“self”时,出现错误。
然后我尝试直接访问列表控件:
self.MainFrame.lc_change.Append(["1","3" + "\n"])
还有:
MainFrame.lc_change.Append(["1","3" + "\n"])
这两个给我“类型对象'xxx'没有属性'xxx'
谁能告诉我我做错了什么?
解决方法
您需要做的是能够允许 DataGrid
引用 wx.ListCtrl
创建的 MainFrame
。
class MainFrame(wx.Frame):
def __init__(self,parent,data): # (self,data):
wx.Frame.__init__(self,-1,"Varkey Foundation") #,size=(640,480))
#Create a panel
self.p = wx.Panel(self)
#Create the ListCtrl here:
self.lc_change = wx.ListCtrl(self.p,style = wx.TE_MULTILINE | wx.LC_REPORT | wx.LC_VRULES)
...
# Later when you create the DataGrid:
#Create the grid and continue layout
self.grid = DataGrid(self.page1,data,self.lc_change) # pass in the ListCtrl
...
class DataGrid(gridlib.Grid):
def __init__(self,lc):
gridlib.Grid.__init__(self,- 1) #,colnames,-1
self.lc = lc
...
def onCellChanged(self,event):
...
self.lc.Append(["1","2","3" + "\n"]) # Now update the ListCtrl
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。