import wx
import wx.grid
class MyFrame(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self,parent,title="工具",size=(500,400))
panel = wx.Panel(self)
# 创建菜单栏
menubar = wx.MenuBar()
# 创建菜单
menu = wx.Menu()
menubar.Append(menu,"文件")
# 创建子菜单
menu.Append(-1,"打开")
menu.Append(-1,"保存")
menu.Append(-1,"另存为")
text = wx.StaticText(panel,label="静态文本")
self.btn = wx.Button(panel, -1, "Quit")
self.btn.Bind(wx.EVT_ENTER_WINDOW,self.enter_window)
self.btn.Bind(wx.EVT_LEAVE_WINDOW,self.leave_window)
self.Bind(wx.EVT_BUTTON, self.OnClick, self.btn)
# 单选框
radioRed = wx.RadioButton(panel, -1, "红")
radioGreen = wx.RadioButton(panel, -1, "绿")
radioBlue = wx.RadioButton(panel, -1, "蓝")
self.colors = {"红": wx.RED, "绿": wx.GREEN, "蓝": wx.BLUE}
self.textColor = wx.TextCtrl(panel, -1, "")
for eachRadio in [radioRed, radioBlue, radioGreen]: # 对每一个按钮都注册事件
eachRadio.Bind(wx.EVT_RADIOBUTTON, self.OnClick2)
# 位图
bmp = wx.Image(r".\0035.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap()
self.bmp_button = wx.BitmapButton(panel, -1, bmp)
# 多选框
# colorList = ["红", "绿", "蓝", "白", "紫"]
# self.listBox = wx.CheckListBox(panel, -1, (10, 10), wx.DefaultSize, colorList,style=wx.LB_SINGLE)
# self.listBox.SetSelection(2)
# # 绑定单击事件
# self.Bind(wx.EVT_LISTBOX, self.OnClickLIStBox, self.listBox)
# 表格
rowTitles = ["第1行", "第2行", "第3行", "第4行"]
colTitles = ["第1列", "第2列", "第3列", "第4列"]
grid = wx.grid.Grid(panel,-1, size=wx.DefaultSize)
grid.CreateGrid(4, 4)
for row in range(4):
grid.SetRowLabelValue(row, rowTitles[row]) # 设置行标题
grid.SetColLabelValue(row, colTitles[row]) # 设置列标题
for col in range(4): # 设置单元格的值
grid.SetCellValue(row, col, str(col))
# 布局
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(text,proportion=1, flag=wx.TOP|wx.BOTTOM, border=10)
sizer.Add(self.btn ,proportion=1, flag=wx.TOP|wx.BOTTOM, border=10)
sizer.Add(self.bmp_button ,proportion=1, flag=wx.TOP|wx.BOTTOM, border=1)
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
sizer2.Add(radioRed, 0)
sizer2.Add(radioGreen, 0)
sizer2.Add(radioBlue, 0)
sizer2.Add( self.textColor, 1)
sizer_sum = wx.BoxSizer(wx.VERTICAL)
sizer_sum.Add(sizer,flag=wx.EXPAND)
sizer_sum.Add(sizer2,flag=wx.EXPAND)
sizer_sum.Add(grid,flag=wx.EXPAND)
panel.SetSizer(sizer_sum)
self.Center()
self.SetMenuBar(menubar)
def OnClick2(self, event): # 只有点击的对象才会触发该函数
radioSelected = event.GetEventObject() # 返回被选中的按钮的对象
str = radioSelected.GetLabel() # 获取当前对象的label值
self.textColor.SetBackgroundColour(self.colors[str])
self.textColor.SetFocus()
def OnClick(self,event):
print("点击成功")
# self.Close(True)
def OnClickLIStBox(self,event):
index = self.listBox.GetSelection()
wx.MessageBox(self.listBox.GetString(index), "提示")
def enter_window(self,event):
print("鼠标进入了")
self.btn.SetToolTip("我是提示。。。")
self.btn.SetLabel("鼠标进入了")
def leave_window(self,event):
print("鼠标离开了")
self.btn.SetLabel("鼠标离开了")
class Myapp(wx.App):
def OnInit(self):
print("进入事件循环")
self.frame = MyFrame(None)
self.frame.Show()
return True
def OnExit(self):
print("事件结束")
return True
app = Myapp()
app.MainLoop()