罪恶的一门课啊,上课讲的都没用,完全靠百度。
#_*_coding:utf-8_*_
import wx, os
CurrentCursor = 1 # 当前使用的图标
ChooseBitmap = [0, 0, 0] # 复选位图标记
Choose = [0, 0, 0] # 复选位图标记的镜像
flag = 0 # 单选位图标记
flag0 = 0 # 单选位图标记的镜像
IsChinese = 1 # 判断当前语言是不是中文
class MyModalDialog(wx.Dialog):
def __init__(self, parent, title):
super(MyModalDialog, self).__init__(parent, title = title, size = (400,300), pos = (500,300)) # 调用父类构造器(必须)
panel = wx.Panel(self)
wx.Button(panel, wx.ID_OK, label = u"确定", size = (100, 30), pos = (200, 60))
wx.Button(panel, wx.ID_CANCEL, label = u"取消", size=(100, 30), pos=(200, 140))
self.check1 = wx.CheckBox(panel, 210, u"复选位图1", pos=(50, 50), size=(100, 30), style=0)
self.check2 = wx.CheckBox(panel, 211, u"复选位图2", pos=(50, 100), size=(100, 30), style=0)
self.check3 = wx.CheckBox(panel, 212, u"复选位图3", pos=(50, 150), size=(100, 30), style=0)
self.Bind(wx.EVT_BUTTON, self.Ok, id=wx.ID_OK)
self.Bind(wx.EVT_BUTTON, self.Cancel, id=wx.ID_CANCEL)
self.Bind(wx.EVT_CHECKBOX, self.Check1, self.check1)
self.Bind(wx.EVT_CHECKBOX, self.Check2, self.check2)
self.Bind(wx.EVT_CHECKBOX, self.Check3, self.check3)
global Choose, ChooseBitmap
for i in range(0, 3): # 保存当前复选位图标记,以便点“取消”按钮的时候恢复
Choose[i] = ChooseBitmap[i]
if ChooseBitmap[0] != 0: # 恢复上次选项
self.check1.SetValue(True)
if ChooseBitmap[1] != 0:
self.check2.SetValue(True)
if ChooseBitmap[2] != 0:
self.check3.SetValue(True)
def Cancel(self, evt): # 如果点击的是“取消”,则恢复上一次的状态
for i in range(0, 3):
ChooseBitmap[i] = Choose[i]
self.Destroy()
def Ok(self, evt): # 如果模式对话框点击的是"确定",就根据标记情况显示复选位图
global Choose, ChooseBitmap
#if ChooseBitmap[0] != 0:
# self.GetParent().bitmap1.SetBitmap(self.temp[0])
#if ChooseBitmap[1] != 0:
# self.GetParent().bitmap2.SetBitmap(self.temp[1])
#if ChooseBitmap[2] != 0:
# self.GetParent().bitmap3.SetBitmap(self.temp[2])
self.Destroy()
def Check1(self, evt): # 处理第一个复选位图项
if ChooseBitmap[0] == 0:
ChooseBitmap[0] = 1
else:
ChooseBitmap[0] = 0
def Check2(self, evt): # 处理第二个复选位图项
if ChooseBitmap[1] == 0:
ChooseBitmap[1] = 1
else:
ChooseBitmap[1] = 0
def Check3(self, evt): # 处理第三个复选位图项
if ChooseBitmap[2] == 0:
ChooseBitmap[2] = 1
else:
ChooseBitmap[2] = 0
class MyModalessDialog(wx.Dialog):
def __init__(self, parent, title):
super(MyModalessDialog, self).__init__(parent, title = title, size = (400,300))
panel = wx.Panel(self)
wx.Button(panel, wx.ID_OK, label=u"确定", size=(100, 30), pos=(200, 60))
wx.Button(panel, wx.ID_CANCEL, label=u"取消", size=(100, 30), pos=(200, 140))
self.check1 = wx.RadioButton(panel, 213, u"单选位图1", pos=(50, 50), size=(100, 30), style=0)
self.check2 = wx.RadioButton(panel, 214, u"单选位图2", pos=(50, 100), size=(100, 30), style=0)
self.check3 = wx.RadioButton(panel, 215, u"单选位图3", pos=(50, 150), size=(100, 30), style=0)
self.Bind(wx.EVT_RADIOBUTTON, self.CheckRadio, id=213, id2=215)
self.Bind(wx.EVT_BUTTON, parent.Ok1, id=wx.ID_OK)
self.Bind(wx.EVT_BUTTON, self.Cancel, id=wx.ID_CANCEL)
global flag, flag0
flag0 = flag # 保存当前选项,以便点击“取消”的时候恢复
if (flag == 0): # 恢复上次选项
self.check1.SetValue(True)
elif (flag == 1):
self.check2.SetValue(True)
else:
self.check3.SetValue(True)
def CheckRadio(self, evt): # 处理单选项
Id = evt.GetId()
global flag
if Id == 213:
flag = 0
elif Id == 214:
flag = 1
else:
flag = 2
def Cancel(self, evt):
global flag, flag0
flag = flag0
self.Destroy()
class MyFrame(wx.Frame):
def __init__(self): # 初始化菜单
wx.Frame.__init__(self, None, -1, u"第六次上机练习(WX)", size=(800, 500))
self.menuBar = wx.MenuBar()
self.file = wx.Menu()
self.file.Append(200, u"退出")
self.Bind(wx.EVT_MENU, self.CloseWindow, id=200)
self.menuBar.Append(self.file, u"&文件(F)")
self.dialog = wx.Menu()
self.dialog.Append(201, u"模式对话框")
self.dialog.Append(202, u"无模式对话框")
self.dialog.Append(203, u"文件对话框")
self.Bind(wx.EVT_MENU_RANGE, self.ShowDialog,id=201,id2=203)
self.menuBar.Append(self.dialog, u"&对话框(D)")
self.cursor = wx.Menu()
self.cursor.AppendRadioItem(204, u"箭头")
self.cursor.AppendRadioItem(205, u"十字")
self.cursor.AppendRadioItem(206, u"自定义")
self.Bind(wx.EVT_MENU_RANGE, self.ShowCursor,id=204,id2=206)
self.menuBar.Append(self.cursor, u"&光标类型(D)")
self.language = wx.Menu()
self.language.AppendRadioItem(207, u"中文")
self.language.AppendRadioItem(208, u"英文")
self.Bind(wx.EVT_MENU_RANGE, self.ChooseLanguage,id=207,id2=208)
self.menuBar.Append(self.language, u"&语言(L)")
self.information = wx.Menu()
self.information.Append(209, u"&程序信息")
self.Bind(wx.EVT_MENU, self.ShowInformation, id=209)
self.menuBar.Append(self.information, u"&关于(A)")
self.SetMenuBar(self.menuBar)
self.panel = wx.Panel(self, -1)
self.text = wx.StaticText(self.panel, -1, u"当前光标是:IDC_ARROW", pos=(200, 100), size=(100, 40))
#font = wx.Font(40, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
#self.text.SetFont(font)
self.bitmap1 = wx.StaticBitmap(self.panel, -1, pos = (100, 40))
self.bitmap2 = wx.StaticBitmap(self.panel, -1, pos = (100, 120))
self.bitmap3 = wx.StaticBitmap(self.panel, -1, pos = (100, 200))
self.bitmap4 = wx.StaticBitmap(self.panel, -1, pos=(400, 200))
self.temp = range(0, 3) # 加载位图
#for i in range(0, 3):
# image = wx.Image('bitmap%d.bmp'%(i+1), wx.BITMAP_TYPE_BMP)
# self.temp[i] = image.ConvertToBitmap()
def ShowDialog(self, evt):
Id = evt.GetId()
if Id == 201: # 模式对话框
MyModalDialog(self, u"复选显示位图选择对话框").ShowModal()
if Id == 202:
self.a = MyModalessDialog(self, u"单选显示位图选择对话框")
self.a.Show()
if Id == 203: # 文件对话框
wildcard = "Python source (*.py)|*.py|" \
"Compiled Python (*.pyc)|*.pyc|" \
"All files (*.*)|*.*"
dialog = wx.FileDialog(None, "Choose a file", os.getcwd(),"", wildcard, wx.OPEN)
if dialog.ShowModal() == wx.ID_OK:
self.text = u"所选文件名:" + dialog.GetPath()
dialog.Destroy()
wx.MessageBox(self.text, u"文件名")
self.Refresh()
def ShowCursor(self, evt):
Id = evt.GetId()
global CurrentCursor
if Id == 204: # 将光标更改为箭头
CurrentCursor = 1
if IsChinese == 1:
self.text.SetLabel(u"当前光标是:ARROW")
else:
self.text.SetLabel(u"Current Cursor:ARROW")
self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
if Id == 205: # 将光标更改为十字
CurrentCursor = 2
if IsChinese == 1:
self.text.SetLabel(u"当前光标是:CROSS")
else:
self.text.SetLabel(u"Current Cursor:CROSS")
self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))
if Id == 206: # 将光标更改为自定义
CurrentCursor = 3
if IsChinese == 1:
self.text.SetLabel(u"当前光标是:UserDefined")
else:
self.text.SetLabel(u"Current Cursor:UserDefined")
self.SetCursor(self.cursor)
self.Refresh()
def ChooseLanguage(self, evt):
Id = evt.GetId()
if Id == 208: # 更改为英文菜单
self.menuBar.SetLabelTop(0, u"File(F)")
self.menuBar.SetLabelTop(1, u"Dialog(D)")
self.menuBar.SetLabelTop(2, u"Cursor(C)")
self.menuBar.SetLabelTop(3, u"Language(L)")
self.menuBar.SetLabelTop(4, u"About(A)")
self.file.SetLabel(200, "Exit")
self.dialog.SetLabel(201, u"Modal Dialog")
self.dialog.SetLabel(202, u"Modaless Dialog")
self.dialog.SetLabel(203, u"File Dialog")
self.cursor.SetLabel(204, u"Arrow")
self.cursor.SetLabel(205, u"Cross")
self.cursor.SetLabel(206, u"UserDefined")
self.language.SetLabel(207, u"Chinese")
self.language.SetLabel(208, u"English")
self.information.SetLabel(209, u"Program Information")
global IsChinese
IsChinese = 0
if CurrentCursor == 1:
self.text.SetLabel(u"Current Cursor:ARROW")
if CurrentCursor == 2:
self.text.SetLabel(u"Current Cursor:CROSS")
if CurrentCursor == 3:
self.text.SetLabel(u"Current Cursor:UserDefined")
if Id == 207: # 更改为中文菜单
self.menuBar.SetLabelTop(0, u"文件(F)")
self.menuBar.SetLabelTop(1, u"对话框(D)")
self.menuBar.SetLabelTop(2, u"光标类型(C)")
self.menuBar.SetLabelTop(3, u"语言(L)")
self.menuBar.SetLabelTop(4, u"关于(A)")
self.file.SetLabel(200, u"退出")
self.dialog.SetLabel(201, u"模式对话框")
self.dialog.SetLabel(202, u"无模式对话框")
self.dialog.SetLabel(203, u"文件对话框")
self.cursor.SetLabel(204, u"箭头")
self.cursor.SetLabel(205, u"十字")
self.cursor.SetLabel(206, u"自定义")
self.language.SetLabel(207, u"中文")
self.language.SetLabel(208, u"英文")
self.information.SetLabel(209, u"程序信息")
global IsChinese
IsChinese = 1
if CurrentCursor == 1:
self.text.SetLabel(u"当前光标是:ARROW")
if CurrentCursor == 2:
self.text.SetLabel(u"当前光标是:CROSS")
if CurrentCursor == 3:
self.text.SetLabel(u"当前光标是:UserDefined")
def ShowInformation(self, evt): # 输出学号等信息
wx.MessageBox(u"第六次上机练习(WX)\n对话框,光标,字符串,位图\n\n学号:***********\n姓名:***",
u"About Menu", wx.OK | wx.ICON_INFORMATION, self)
#def Ok(self, evt): # 如果模式对话框点击的是"确定",就根据标记情况显示复选位图
#global Choose, ChooseBitmap
#if ChooseBitmap[0] != 0:
# self.bitmap1.SetBitmap(self.temp[0])
#if ChooseBitmap[1] != 0:
# self.bitmap2.SetBitmap(self.temp[1])
#if ChooseBitmap[2] != 0:
# self.bitmap3.SetBitmap(self.temp[2])
#for i in range(0, 3):
# Choose[i] = ChooseBitmap[i]
def Ok1(self, evt): # 如果无模式对话框点击的是“确定”,就根据flag的值显示位图
#if flag == 0:
# self.bitmap4.SetBitmap(self.temp[0])
#elif flag == 1:
# self.bitmap4.SetBitmap(self.temp[1])
#else:
# self.bitmap4.SetBitmap(self.temp[2])
self.a.Destroy()
def CloseWindow(self, evt):
self.Close()
if __name__ == u'__main__':
app = wx.PySimpleApp()
frame = MyFrame()
frame.Show(True)
app.MainLoop()