教你用 AI 做一个写歌词的软件 歌词软件( 二 )

2、功能调用:我们使用按钮中的command参数调用已设置好的函数即可 。其中函数部分我们通过生成文本和删除文本的方式读入数据和写入数据 。为了防止数据重叠故在要时刻监测重复软件 。定义的函数内容如下:
def song:ss=entry.getf=open(1.txt,w)f.write(ss)f.closeos.startfile(1.bat)while True:if os.path.exists(2.txt):f=open(2.txt)ws=f.readf.closeentry1.insert(0.0, ws)breaktry:os.remove(1.txt)os.remove(2.txt)except:pass3、GUI代码:
整个GUI界面代码如下:
import tkinter as tkfrom PIL import ImageTk, Imageimport ostry:os.remove(1.txt)os.remove(2.txt)except:passimport osdef song:ss=entry.getf=open(1.txt,w)f.write(ss)f.closeos.startfile(1.bat)while True:if os.path.exists(2.txt):f=open(2.txt)ws=f.readf.closeentry1.insert(0.0, ws)breaktry:os.remove(1.txt)os.remove(2.txt)except:passroot = tk.Tkroot.title('AI写歌词')# 背景canvas = tk.Canvas(root, width=800, height=500, bd=0, highlightthickness=0)imgpath = '1.jpg'img = Image.open(imgpath)photo = ImageTk.PhotoImage(img)imgpath2 = '3.jpg'img2 = Image.open(imgpath2)photo2 = ImageTk.PhotoImage(img2)canvas.create_image(700, 400, image=photo)canvas.packlabel=tk.Label(text=请输入关键词:,font=(微软雅黑,20))entry = tk.Entry(root, insertbackground='blue', highlightthickness=2,font=(微软雅黑,15))entry.packentry1 = tk.Text(height=15,width=115)entry1.packbnt = tk.Button(width=15,height=2,image=photo2,command=song)canvas.create_window(100, 50, width=200, height=30,window=label)canvas.create_window(500, 50, width=630, height=30,window=entry)canvas.create_window(400, 100, width=220, height=50,window=bnt)canvas.create_window(400, 335, width=600, height=400,window=entry1)root.mainloop【教你用 AI 做一个写歌词的软件 歌词软件】到这里,我们整体的程序就搭建完成,下面为我们程序的运行过程和结果:

教你用 AI 做一个写歌词的软件 歌词软件


教你用 AI 做一个写歌词的软件 歌词软件



推荐阅读