網站首頁 編程語言 正文
使用tkFileDialog實現文件選擇、保存和路徑選擇
概述
看了下Tkinter的文檔,對于Pop-up dialog有三類,現在用到的是tkFileDialog
tkFileDialog有三種形式:
- 一個是:askopenfilename(option=value, …) 這個是”打開”對話框
- 一個是:asksaveasfilename(option=value, …) 這個是另存為對話框
- 另一個是:askdirectory()這個是路徑選擇對話框
option參數如下:
-
defaultextension = s
默認文件的擴展名 -
filetypes = [(label1, pattern1), (label2, pattern2), …]
設置文件類型下拉菜單里的的選項 -
initialdir = D
對話框中默認的路徑 -
initialfile = F
對話框中初始化顯示的文件名 -
parent = W
父對話框(由哪個窗口彈出就在哪個上端) -
title = T
彈出對話框的標題
如果選中文件的話,確認后會顯示文件的完整路徑,否則單擊取消的話會返回空字符串
示例
#coding=UTF-8 import Tkinter, Tkconstants, tkFileDialog class TkFileDialogExample(Tkinter.Frame): def __init__(self, root): Tkinter.Frame.__init__(self, root) # options for buttons button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5} # define buttons Tkinter.Button(self, text='askopenfile', command=self.askopenfile).pack(**button_opt) Tkinter.Button(self, text='askopenfilename', command=self.askopenfilename).pack(**button_opt) Tkinter.Button(self, text='asksaveasfile', command=self.asksaveasfile).pack(**button_opt) Tkinter.Button(self, text='asksaveasfilename', command=self.asksaveasfilename).pack(**button_opt) Tkinter.Button(self, text='askdirectory', command=self.askdirectory).pack(**button_opt) # define options for opening or saving a file self.file_opt = options = {} options['defaultextension'] = '.txt' options['filetypes'] = [('all files', '.*'), ('text files', '.txt')] options['initialdir'] = 'C:\\' options['initialfile'] = 'myfile.txt' options['parent'] = root options['title'] = 'This is a title' # This is only available on the Macintosh, and only when Navigation Services are installed. #options['message'] = 'message' # if you use the multiple file version of the module functions this option is set automatically. #options['multiple'] = 1 # defining options for opening a directory self.dir_opt = options = {} options['initialdir'] = 'C:\\' options['mustexist'] = False options['parent'] = root options['title'] = 'This is a title' def askopenfile(self): """Returns an opened file in read mode.""" return tkFileDialog.askopenfile(mode='r', **self.file_opt) def askopenfilename(self): """Returns an opened file in read mode. This time the dialog just returns a filename and the file is opened by your own code. """ # get filename filename = tkFileDialog.askopenfilename(**self.file_opt) # open file on your own if filename: return open(filename, 'r') def asksaveasfile(self): """Returns an opened file in write mode.""" return tkFileDialog.asksaveasfile(mode='w', **self.file_opt) def asksaveasfilename(self): """Returns an opened file in write mode. This time the dialog just returns a filename and the file is opened by your own code. """ # get filename filename = tkFileDialog.asksaveasfilename(**self.file_opt) # open file on your own if filename: return open(filename, 'w') def askdirectory(self): """Returns a selected directoryname.""" return tkFileDialog.askdirectory(**self.dir_opt) if __name__ == '__main__': root = Tkinter.Tk() TkFileDialogExample(root).pack() root.mainloop()
ImportError: No module named 'tkFileDialog'問題
原因
python2和pyton3的版本問題。python3之后的版本自帶有tkinter.
驗證
import _tkinter
import tkinter
tkinter._test()
在python3中輸入以上命令進行驗證。
解決方法
Python2中應該寫成 ?
from tkFileDialog import askdirectory
python3中應該寫成 ?
from tkinter.filedialog import askdirectory
tkColorChooser ------------>tkinter.colorchooser
tkCommonDialog --------------->tkinter.commondialog ??
其他的可以類推。
原文鏈接:https://blog.csdn.net/weixin_44256803/article/details/86571383
相關推薦
- 2022-09-17 利用Python實現快速批量轉換HEIC文件_python
- 2022-03-20 Redis實現分布式鎖方法(redis分布式鎖的實現原理)
- 2022-06-29 python人工智能tensorflow函數tf.assign使用方法_python
- 2022-07-06 python?pandas中的agg函數用法_python
- 2022-03-27 Android自定義模擬時鐘控件_Android
- 2022-07-16 Spring Boot之處理各類請求和響應
- 2022-07-30 react組件的生命周期
- 2023-02-10 解決Jupyter?Notebook?“signal?only?works?in?main?thre
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支