#!/usr/bin/env python3
# encoding: utf-8
'''
Created on 2018年8月24日
@author: bdwjh
'''
import wx # 导入wxPython
import os
class Frame(wx.Frame): # 定义GUI框架类
wildcard='文本文件(*.txt)|*.txt|所有文件(*.*)|*.*'
# 框架初始化方法
def __init__(self, parent=None, id=-1, pos=wx.DefaultPosition,
title='wxPython 文件对话框使用详解'):
wx.Frame.__init__(self, parent, id, title,
pos, size=(600, 300))
self.panel = wx.Panel(self)
self.openBtn = wx.Button(self.panel, -1, '打开', pos=(50, 100))
self.openBtn.Bind(wx.EVT_BUTTON, self.OnOpen)
self.saveAsBtn = wx.Button(self.panel, -1, '另存为', pos=(150, 100))
self.saveAsBtn.Bind(wx.EVT_BUTTON, self.OnSaveAs)
def OnOpen(self, event):
dlg = wx.FileDialog(self, message='打开文件',
defaultDir='',
defaultFile='',
wildcard=self.wildcard,
style=wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_OK:
file = dlg.GetPath()
print(file)
dlg.Destroy()
def OnSaveAs(self, event):
dlg = wx.FileDialog(self, '另存为', os.getcwd(),
defaultFile='我的文件.txt',
style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT,
wildcard=self.wildcard)
if dlg.ShowModal() == wx.ID_OK:
file = dlg.GetPath()
print(file)
dlg.Destroy()
class App(wx.App): # 定义应用程序类
def OnInit(self): # 类初始化方法
self.frame = Frame()
self.frame.Show(True)
self.SetTopWindow(self.frame) # 设置顶层框架
return True
def main(): # 定义主函数用于启动GUI界面
app = App()
app.MainLoop()
if __name__ == '__main__': # 使用__name__检测当前模块
main()