目前能做到弹出文本对话框,但是不知道这么读取txt文件的内容,求解答
我目前的代码如下
import winsound
from tkinter import Tk ,filedialog
from PyQt5.QtWidgets import QDialog, QFileDialog
from tkinter import *
def first():
for i in range(1,20):
i=i*100
winsound.Beep(i,500) #定义i从1到20,相当于频率从0到2000HZ
def Readfile():
root = Tk()
root.withdraw()
cur = filedialog.askopenfilenames(filetypes=[('text files', '.txt'), ('pythonfiles', ('.py', '.pyw'))])
if cur:
print(cur)
else:
print('你没有选择任何文件')
def submit():#定义读取乐谱并播放的函数
music = list(map(str,u.get()))
order =[[131,147,165,175,196,220,247],[262,296,330,349,392,440,494],[523,587,659,698,784,880,988],[1047,1175,1319,1397,1568,1760,1976]]#将不同的频率从低到高存到数组里
match =['0','1','2','3','4','5','6','7','8','9']
char =[ 'A','B','C','D']#用字母表示所在音区 A为低音区 B为中音区 C为高音 D为高二度
n = 1
j=0
k=0
m=0
time1 = [0 for x in range(10,1000)]
order1 = [0 for x in range(0,1000)]
order11 = [0 for x in range(0,1000)]
orderm = [0 for x in range(0,1000)]
for i in range(len(music)):
if music[i] == '/': #当输入读到/时,表示为两个音符的间隔,进行分块,读入前一个
n=n+1
if i==0:
if music[i] == '0' and music[i+1] == '.' and music[i+2] == '5':#定义0.5节拍
time1[j]=0.5
j=j+1
t=0
while t<10:
if music[i] == match[t] and music[i+3] == '/':
time1[j]=t
j=j+1
t=t+1
else:
if music[i] == '0' :
time1[j]=0.5
j=j+1
for tp in range(1,9):
if (music[i] == match[tp] and music[i-1] == '/' ):
time1[j]=tp
j=j+1
for tn in range(0,4):
if music[i] == char[tn] :
order1[m]=tn
m=m+1
for tt in range(1,9):
if music[i] == match[tt] and ( music[i-1] == 'A' or music[i-1] == 'B' or music[i-1] == 'C' or music[i-1] == 'D') :
order11[k]=tt
k=k+1
#print(n)
l=0
while l< n:
time1[l]=time1[l]*int(p.get())
orderm[l]=order[order1[l]][order11[l]-1]
time1[l]=int(time1[l])
l=l+1
l=0
while l <n:
winsound.Beep(orderm[l],time1[l])
l=l+1
#图形界面
root = Tk()
root.title("电子钢琴")
frame = Frame(root)
frame.pack(padx=8, pady=8, ipadx=4)
lab1 = Label(frame, text="文本:")
lab1.grid(row=0, column=0, padx=5, pady=5, sticky=W)
u = StringVar()
ent1 = Entry(frame, textvariable=u)
ent1.grid(row=0, column=1, sticky='ew', columnspan=2)
lab2 = Label(frame, text="请输入每个音节代表的时间(单位ms):")
lab2.grid(row=1, column=0, padx=5, pady=5, sticky=W)
p = StringVar()
ent2 = Entry(frame, textvariable=p)
ent2.grid(row=1, column=1, sticky='ew', columnspan=2)
button = Button(frame, text="确认", command=submit, default='active')
button.grid(row=2, column=1)
button2 = Button(frame, text="读入文本", command=Readfile)
button2.grid(row=2, column=2, padx=5, pady=5)
button3 = Button(frame, text="播放第一题",command=first)
button3.grid(row=2, column=0)
root.mainloop()