欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中使用tkFileDialog實(shí)現(xiàn)文件選擇、保存和路徑選擇

 更新時(shí)間:2022年05月20日 11:42:31   作者:blingbling_110  
這篇文章主要介紹了Python中使用tkFileDialog實(shí)現(xiàn)文件選擇、保存和路徑選擇,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

使用tkFileDialog實(shí)現(xiàn)文件選擇、保存和路徑選擇

概述

看了下Tkinter的文檔,對(duì)于Pop-up dialog有三類,現(xiàn)在用到的是tkFileDialog

tkFileDialog有三種形式:

  • 一個(gè)是:askopenfilename(option=value, …) 這個(gè)是”打開(kāi)”對(duì)話框
  • 一個(gè)是:asksaveasfilename(option=value, …) 這個(gè)是另存為對(duì)話框
  • 另一個(gè)是:askdirectory()這個(gè)是路徑選擇對(duì)話框

option參數(shù)如下:

  • defaultextension = s 默認(rèn)文件的擴(kuò)展名
  • filetypes = [(label1, pattern1), (label2, pattern2), …] 設(shè)置文件類型下拉菜單里的的選項(xiàng)
  • initialdir = D 對(duì)話框中默認(rèn)的路徑
  • initialfile = F 對(duì)話框中初始化顯示的文件名
  • parent = W 父對(duì)話框(由哪個(gè)窗口彈出就在哪個(gè)上端)
  • title = T 彈出對(duì)話框的標(biāo)題

如果選中文件的話,確認(rèn)后會(huì)顯示文件的完整路徑,否則單擊取消的話會(huì)返回空字符串

示例

#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'問(wèn)題

原因

python2和pyton3的版本問(wèn)題。python3之后的版本自帶有tkinter.

驗(yàn)證

  • import _tkinter
  • import tkinter
  • tkinter._test()

在python3中輸入以上命令進(jìn)行驗(yàn)證。

解決方法

Python2中應(yīng)該寫成  

from tkFileDialog import askdirectory

python3中應(yīng)該寫成  

from tkinter.filedialog import askdirectory

tkColorChooser ------------>tkinter.colorchooser
tkCommonDialog --------------->tkinter.commondialog   

其他的可以類推。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Python變量與注釋高級(jí)用法

    詳解Python變量與注釋高級(jí)用法

    變量與注釋是表達(dá)作者思想的基礎(chǔ),他們對(duì)代碼質(zhì)量的貢獻(xiàn)母庸質(zhì)疑,這篇文章主要介紹了Python變量與注釋高級(jí)用法,需要的朋友可以參考下
    2022-08-08
  • Python探索之ModelForm代碼詳解

    Python探索之ModelForm代碼詳解

    這篇文章主要介紹了Python探索之ModelForm代碼詳解,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • Python爬取數(shù)據(jù)保存為Json格式的代碼示例

    Python爬取數(shù)據(jù)保存為Json格式的代碼示例

    今天小編就為大家分享一篇關(guān)于Python爬取數(shù)據(jù)保存為Json格式的代碼示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-04-04
  • 導(dǎo)致python中import錯(cuò)誤的原因是什么

    導(dǎo)致python中import錯(cuò)誤的原因是什么

    在本篇文章里小編給大家整理了關(guān)于python的import錯(cuò)誤原因以及相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-07-07
  • 詳解python eval函數(shù)的妙用

    詳解python eval函數(shù)的妙用

    這篇文章主要介紹了詳解python eval函數(shù)的妙用,詳細(xì)介紹了python eval函數(shù)的具體用法和實(shí)例,有興趣的可以了解一下
    2017-11-11
  • 淺談Tensorflow模型的保存與恢復(fù)加載

    淺談Tensorflow模型的保存與恢復(fù)加載

    本篇文章主要介紹了淺談Tensorflow模型的保存與恢復(fù)加載,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • python分批定量讀取文件內(nèi)容,輸出到不同文件中的方法

    python分批定量讀取文件內(nèi)容,輸出到不同文件中的方法

    今天小編就為大家分享一篇python分批定量讀取文件內(nèi)容,輸出到不同文件中的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • Pytorch中torchtext終極安裝方法以及常見(jiàn)問(wèn)題

    Pytorch中torchtext終極安裝方法以及常見(jiàn)問(wèn)題

    torchtext是pytorch框架中用于文本處理的,下面這篇文章主要給大家介紹了關(guān)于Pytorch中torchtext終極安裝方法以及常見(jiàn)問(wèn)題的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Flask搭建一個(gè)API服務(wù)器的步驟

    Flask搭建一個(gè)API服務(wù)器的步驟

    Flask真是一個(gè)強(qiáng)大且簡(jiǎn)介的web框架,能夠快速搭建web服務(wù)器,本文主要介紹了Flask搭建一個(gè)API服務(wù)器的步驟,分享給大家,感興趣的可以了解一下
    2021-05-05
  • Python?selenium?webdriver?基本使用代碼分享

    Python?selenium?webdriver?基本使用代碼分享

    這篇文章主要介紹了Python?selenium?webdriver?基本使用,本文僅僅簡(jiǎn)單介紹了selenium的使用常用代碼分享,需要的朋友可以參考下
    2022-11-11

最新評(píng)論