网络编程
位置:首页>> 网络编程>> Python编程>> Python3 tkinter 实现文件读取及保存功能

Python3 tkinter 实现文件读取及保存功能

作者:LOONGV  发布时间:2023-10-24 19:45:46 

标签:python,tkinter,文件,读取,保存

tkinter介绍

tkinter是python自带的GUI库,是对图形库TK的封装

tkinter是一个跨平台的GUI库,开发的程序可以在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('窗口标题') # 标题
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() # 显示

Python3 tkinter 实现文件读取及保存功能Python3 tkinter 实现文件读取及保存功能

总结

以上所述是小编给大家介绍的Python3 tkinter 实现文件读取及保存功能,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

来源:https://blog.csdn.net/u013032852/article/details/93996709

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com