import os import tkinter as tk from tkinter import filedialog from tkinter import messagebox #获取需要转换的txt文件路径 def file_path(): filepath = filedialog.askopenfilename() #定义全局变量 global path1 path1=filepath filename1.set(filepath) #定义转换格式函数 def change(): with open(path1,encoding='gbk') as f: res = f.readlines() vcfpath=os.path.dirname(path1) '/通讯录.vcf' with open(vcfpath,"w",encoding='utf-8')as v: for r in res: nn = r.split("\t") v.write("BEGIN:VCARD" "\n") v.write("VERSION:3.0" "\n") v.write("FN;CHARSET=utf-8:" nn[0] "\n") v.write("FN;CHARSET=utf-8:" nn[0] "\n") v.write("TEL:" nn[1]) v.write("END:VCARD" "\n") messagebox.showinfo(提示,数据转换成功 window = tk.Tk() window.title("通讯录格式转换") window.geometry('500x400 300 300') filename1 = tk.StringVar() path1='' entry = tk.Entry(window, textvariable=filename1) entry.grid(row=1, column=0, padx=5, pady=5) tk.Button(window, text=选择需要转换的文件, command=file_path).grid(row=1, column=1, padx=5, pady=5) bt=tk.Button(window, text=开始转换格式, command=change) bt.grid(padx=50,pady=50) window.mainloop()