Wxpython
资料收集于网络,如有冒犯,敬请告知
wxFormBuilder
并列结构—拖拉
界面卡顿
创建线线(注意将函数另打包进另一函数,否则卡顿)
#线程执行完毕被调用
def Task_Over(self):
print("thread is over")
#函数打包进thread1
def thread1(self):
Get_SeasonData(self)
启动线程
def get_fundstocks( self, event ):
t1 = threading.Thread(target =self.thread1 )
t1.start()
2.线程最后添加wx.CallAfter()
import wx
def Get_SeasonData(Parent):
#*******************
if Parent != None:
wx.CallAfter(Parent.Task_Over)
menu
dialog
wxformBuilder 创建对话框
dlg = dialog.MyDialog1(self)
dlg.Show()
grid
https://blog.csdn.net/jhg1204/article/details/46621877?locationNum=8
grid带勾选框设置:
.GridCellBoolEditor()
attr = wx.grid.GridCellAttr()
attr.SetEditor(wx.grid.
GridCellBoolEditor
())
attr.SetRenderer(wx.grid.
GridCellBoolRenderer(
))
self.m_grid1.SetColAttr(1, attr)
获取带勾选选项
#获取列标签名
m = self.m_grid1.GetRowLabelValue(3)
print(m)
#获取列数
for i in range(self.m_grid1.
GetNumberRows()):
d = self.m_grid1.
GetCellValue
(i,1)
print(d)
grid添加数据
1.添加下列
#
新建PandasToGrid.py
文件添加
import wx.grid as grid
import pandas as pd
class GridTable(grid.PyGridTableBase):
def __init__(self, datas):
grid.GridTableBase.__init__(self)
self.datas = datas
self.isModified = False
self.odd = grid.GridCellAttr()
self.odd.SetReadOnly(False)
self.odd.SetBackgroundColour('yellow')
self.even = grid.GridCellAttr()
self.even.SetReadOnly(False)
def SetValue(self, row, col, value):
print(str(row) + ";" + str(col) + ";" + value)
def innerSetValue(row, col, value):
try:
self.datas[row][col] = value
except IndexError:
# add a new row
self.datas.append([''] * self.GetNumberCols())
innerSetValue(row, col, value)
# tell the grid we've added a row
msg = grid.GridTableMessage(self, # The table
grid.GRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
1 # how many
)
self.GetView().ProcessTableMessage(msg)
innerSetValue(row, col, value)
def GetAttr(self, row, col, kind):
attr = [self.even, self.odd][row % 2]
attr.IncRef()
return attr
def GetNumberRows(self):
return len(self.datas)
def GetNumberCols(self):
return len(self.colLabels)
def GetColLabelValue(self, col):
return self.colLabels[col]
def GetRowLabelValue(self, row):
return str(row)
def GetValue(self, row, col):
return self.datas[row][col]
def IsModified(self):
return self.isModified
def InsertRows(self, pos=1, newData=None):
if newData is None:
newData = [u'', u'', u'', u'', u'']
self.datas.insert(pos, newData)
self.isModified = True
gridView = self.GetView()
gridView.BeginBatch()
insertMsg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_NOTIFY_ROWS_INSERTED, pos, 1)
gridView.ProcessTableMessage(insertMsg)
gridView.EndBatch()
getValueMsg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
gridView.ProcessTableMessage(getValueMsg)
# if self.onGridValueChanged:
# self.onGridValueChanged()
return True
def AppendRows(self, newData=None):
if newData is None:
newData = [u'', u'', u'', u'', u'']
self.datas.append(newData)
self.isModified = True
gridView = self.GetView()
gridView.BeginBatch()
appendMsg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED, 1)
gridView.ProcessTableMessage(appendMsg)
gridView.EndBatch()
getValueMsg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
gridView.ProcessTableMessage(getValueMsg)
# if self.onGridValueChanged:
# self.onGridValueChanged()
return True
def DeleteRows(self, pos=0, numRows=1):
if self.datas is None or len(self.datas) == 0:
return False
for rowNum in range(0, numRows):
self.datas.remove(self.datas[pos + rowNum])
gridView = self.GetView()
gridView.BeginBatch()
deleteMsg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, pos, numRows)
gridView.ProcessTableMessage(deleteMsg)
gridView.EndBatch()
getValueMsg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
gridView.ProcessTableMessage(getValueMsg)
# if self.onGridValueChanged:
# self.onGridValueChanged()
return True
#将pandas 数据显示于grid控件中
def PandasToGrid(grid,df):
#数据表设置
gridDatas = df.values
Tdata = GridTable(gridDatas)
#设置标题
Tdata.colLabels = list(map(lambda x:str(x) ,df.columns))
grid.SetTable(Tdata, True)
grid.
Refresh
()
2.添加数据
#
启动.py
import wx
import 界面 #gui界面文件
##from 对话框 import MyDialog1
import threading
import pandas as pd
from PandasToGrid import PandasToGrid
class MyFrame(界面.MyFrame1): #gui类名
def __init__(self,parent):
界面.MyFrame1.__init__(self,parent) #界面
# Virtual event handlers, overide them in your derived class
def click( self, event ):
df = pd.read_csv('mergy.csv')
PandasToGrid
(self.m_grid1,df)
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame(None)
frame.Show(True)
app.MainLoop()
刷新Refresh
self.Refresh()