程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python operation file dialog box - wxPython

編輯:Python

Python Operation file dialog box ——wxPython


Some time ago, I learned how to operate excel I want to find a way to operate the file dialog box , I have searched a lot on the Internet with the help of tinker The open , In this way, I feel that the operation is a little slow , And there will be an extra root window , Of course it can be closed , But it's more complicated . There is no way to deal with cases involving multiple file selections .

I'm looking at wxpython modular , There are also file dialog box operations , And it is simple and easy to use , So record it here .

rely on :

pip install wxpython

1、 establish app and frame object

app = wx.App()
frame = wx.Frame(None, title="", pos=(0, 0), size=(100, 100))

This step is just because wx.FileDialog Methods need to be created based on the parent container object , Here I choose to create a frame, there pos、size And other parameters can be set to any value , The main thing is to create such an object . There will be no extra windows .
Be careful :app Objects must be created first and must have variables to accept , Otherwise, there will be the following error .

wx._core.PyNoAppError: The wx.App object must be created first!

2、 Create file dialog box

dlg = wx.FileDialog(parent=frame, message=" Multiple file selection ",
defaultDir=os.getcwd(),
style=wx.FD_MULTIPLE,
wildcard="All files*.*|*.*")

Parameter description :

  • parent: Parent container , Here we use the frame
  • message: Dialog title
  • defaultDir: default directory
  • defaultFile: Default file
  • wildcard: File filter , The format is actually very simple “ The display name | Match type ” For a group , Continue to use... When setting multiple groups | Just separate .
  • style: File dialog type
type explain wx.FD_OPEN File selection dialog , For single file selection wx.FD_MULTIPLE File selection dialog , For multi file selection wx.FD_CHANGE_DIR File selection dialog , After the user selects the file, the default directory will be changed wx.FD_SAVE File Save dialog , For multi file selection wx.FD_OVERWRITE_PROMPT Use with the file save dialog , When the file already exists, you will be prompted to overwrite

wx.FD_OVERWRITE_PROMPT example : wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT

3、 Show dialog , Get selected files

if dlg.ShowModal() == wx.ID_OK:
filename = dlg.GetPaths()
print(filename)
dlg.Destroy()

Be careful :dlg.GetPaths The method can only be in wx.FD_MULTIPLE Type , Single file selection requires dlg.GetPath Method .

In fact, there are already many wxpython File operation blog , But most of them are based on integration wx.frame To operate the , When we only need to select the file access path , It is OK to operate like me .

Complete code :

import os
import wx
app = wx.App()
def openFileDialog(style=wx.FD_OPEN, message=" Select File ", defaultDir=os.getcwd(), wildcard="All files(*.*)|*.*"):
frame = wx.Frame(None, title="", pos=(0, 0), size=(100, 100))
dlg = wx.FileDialog(parent=frame, message=message,
defaultDir=defaultDir,
style=style,
wildcard=wildcard)
if dlg.ShowModal() == wx.ID_OK:
if style == wx.FD_MULTIPLE:
return dlg.GetPaths()
return dlg.GetPath()
dlg.Destroy()
return None
print(openFileDialog(wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT))

I have a little encapsulation here , Return to the selected path .
Welcome to leave a message to discuss .


Summary reference blog :https://blog.csdn.net/weixin_30871293/article/details/96099514
More reference :https://www.cnblogs.com/math98/p/14221551.html


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved