Python3 tkinter 實現(xiàn)文件讀取及保存功能
tkinter介紹
tkinter是python自帶的GUI庫,是對圖形庫TK的封裝
tkinter是一個跨平臺的GUI庫,開發(fā)的程序可以在win,linux或者mac下運行
# !/user/bin/env Python3 # -*- coding:utf-8 -*- """ file:window.py.py create time:2019/6/27 14:54 author:Loong Xu desc: 窗口 """ import tkinter as tk from tkinter import filedialog, dialog import os window = tk.Tk() window.title('窗口標(biāo)題') # 標(biāo)題 window.geometry('500x500') # 窗口尺寸 file_path = '' file_text = '' text1 = tk.Text(window, width=50, height=10, bg='orange', font=('Arial', 12)) text1.pack() def open_file(): ''' 打開文件 :return: ''' global file_path global file_text file_path = filedialog.askopenfilename(title=u'選擇文件', initialdir=(os.path.expanduser('H:/'))) print('打開文件:', file_path) if file_path is not None: with open(file=file_path, mode='r+', encoding='utf-8') as file: file_text = file.read() text1.insert('insert', file_text) def save_file(): global file_path global file_text file_path = filedialog.asksaveasfilename(title=u'保存文件') print('保存文件:', file_path) file_text = text1.get('1.0', tk.END) if file_path is not None: with open(file=file_path, mode='a+', encoding='utf-8') as file: file.write(file_text) text1.delete('1.0', tk.END) dialog.Dialog(None, {'title': 'File Modified', 'text': '保存完成', 'bitmap': 'warning', 'default': 0, 'strings': ('OK', 'Cancle')}) print('保存完成') bt1 = tk.Button(window, text='打開文件', width=15, height=2, command=open_file) bt1.pack() bt2 = tk.Button(window, text='保存文件', width=15, height=2, command=save_file) bt2.pack() window.mainloop() # 顯示
總結(jié)
以上所述是小編給大家介紹的Python3 tkinter 實現(xiàn)文件讀取及保存功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Dockerfile構(gòu)建一個Python Flask 鏡像
這篇文章主要介紹了Dockerfile構(gòu)建一個Python Flask 鏡像,對正在學(xué)習(xí)的你有一定的參考價值,需要的小伙伴可以參考一下2022-01-01淺談python socket函數(shù)中,send與sendall的區(qū)別與使用方法
下面小編就為大家?guī)硪黄獪\談python socket函數(shù)中,send與sendall的區(qū)別與使用方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05python 環(huán)境搭建 及python-3.4.4的下載和安裝過程
這篇文章主要介紹了python 環(huán)境搭建 python-3.4.4的下載和安裝過程,文中給大家補充介紹了pycharm的基本用法,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-07-07python matplotlib工具欄源碼探析二之添加、刪除內(nèi)置工具項的案例
這篇文章主要介紹了python matplotlib工具欄源碼探析二之添加、刪除內(nèi)置工具項的案例,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02使用python?scrapy爬取天氣并導(dǎo)出csv文件
由于工作需要,將爬蟲的文件要保存為csv,以前只是保存為json,下面這篇文章主要給大家介紹了關(guān)于如何使用python?scrapy爬取天氣并導(dǎo)出csv文件的相關(guān)資料,需要的朋友可以參考下2022-08-08