最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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有三類(lèi),現(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è)置文件類(lèi)型下拉菜單里的的選項(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)該寫(xiě)成  

from tkFileDialog import askdirectory

python3中應(yīng)該寫(xiě)成  

from tkinter.filedialog import askdirectory

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

其他的可以類(lèi)推。

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

相關(guān)文章

  • python3.7將代碼打包成exe程序并添加圖標(biāo)的方法

    python3.7將代碼打包成exe程序并添加圖標(biāo)的方法

    這篇文章主要介紹了python3.7將代碼打包成exe程序并添加圖標(biāo)的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-10-10
  • 詳解python之協(xié)程gevent模塊

    詳解python之協(xié)程gevent模塊

    這篇文章主要介紹了詳解python之協(xié)程gevent模塊,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • 使用Python實(shí)現(xiàn)微信拍一拍功能的思路代碼

    使用Python實(shí)現(xiàn)微信拍一拍功能的思路代碼

    這篇文章主要介紹了使用Python實(shí)現(xiàn)微信“拍一拍”的思路代碼,,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 關(guān)于python爬蟲(chóng)的原理解析

    關(guān)于python爬蟲(chóng)的原理解析

    這篇文章主要介紹了python爬蟲(chóng)原理,今天我們要向大家詳細(xì)解說(shuō)python爬蟲(chóng)原理,什么是python爬蟲(chóng),python爬蟲(chóng)工作的基本流程是什么等內(nèi)容,希望對(duì)這正在進(jìn)行python爬蟲(chóng)學(xué)習(xí)的同學(xué)有所幫助
    2023-07-07
  • 關(guān)于matplotlib及相關(guān)cmap參數(shù)的取值方式

    關(guān)于matplotlib及相關(guān)cmap參數(shù)的取值方式

    這篇文章主要介紹了關(guān)于matplotlib及相關(guān)cmap參數(shù)的取值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • python實(shí)現(xiàn)提取COCO,VOC數(shù)據(jù)集中特定的類(lèi)

    python實(shí)現(xiàn)提取COCO,VOC數(shù)據(jù)集中特定的類(lèi)

    這篇文章主要介紹了python實(shí)現(xiàn)提取COCO,VOC數(shù)據(jù)集中特定的類(lèi),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • pycharm沒(méi)有找到manage?repositories按鈕的解決辦法

    pycharm沒(méi)有找到manage?repositories按鈕的解決辦法

    這篇文章主要給大家介紹了關(guān)于pycharm沒(méi)有找到manage?repositories按鈕的解決辦法,pycharm是用來(lái)寫(xiě)python的可視化代碼軟件,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • PyInstaller將Python腳本打包為.exe可執(zhí)行文件的步驟詳解

    PyInstaller將Python腳本打包為.exe可執(zhí)行文件的步驟詳解

    Python是一門(mén)強(qiáng)大而靈活的編程語(yǔ)言,為了方便共享和部署,我們可以將 Python 腳本打包為可執(zhí)行文件(.exe),這樣其他用戶就無(wú)需安裝 Python環(huán)境,直接運(yùn)行可執(zhí)行文件即可,本文將介紹如何使用PyInstaller 工具實(shí)現(xiàn)這一目標(biāo),需要的朋友可以參考下
    2023-12-12
  • 如何使用Python?VTK繪制線條

    如何使用Python?VTK繪制線條

    這篇文章主要介紹了如何使用Python-VTK繪制線條,主要繪制直線和曲線,下面文章詳細(xì)實(shí)現(xiàn)過(guò)程需要的小伙伴可以參考一下
    2022-04-04
  • Python插件機(jī)制實(shí)現(xiàn)詳解

    Python插件機(jī)制實(shí)現(xiàn)詳解

    這篇文章主要介紹了Python插件機(jī)制實(shí)現(xiàn)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05

最新評(píng)論

剑河县| 凤山县| 岗巴县| 怀安县| 嵊泗县| 登封市| 女性| 咸宁市| 大庆市| 龙胜| 达日县| 南充市| 河津市| 普定县| 同心县| 锡林浩特市| 丽江市| 台中县| 兖州市| 普兰店市| 麟游县| 宝应县| 唐河县| 大关县| 桃园市| 香港 | 大埔区| 吉木萨尔县| 澄江县| 调兵山市| 安仁县| 陇川县| 吕梁市| 扎兰屯市| 焦作市| 四川省| 布尔津县| 舒城县| 平顶山市| 定日县| 永寿县|