dfsljkjafdsk; 2022-05-15 20:10 采纳率: 100%
浏览 77
已结题

python基于tkinter的电子琴GUI界面设计,不会调用调用文本选择对话框,并且读入txt文件的内容

目前能做到弹出文本对话框,但是不知道这么读取txt文件的内容,求解答

img


我目前的代码如下

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()

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-05-15 20:51
    关注
    def Readfile():
        root = Tk()
        root.withdraw()
        cur = filedialog.askopenfilenames(filetypes=[('text files', '.txt'), ('pythonfiles', ('.py', '.pyw'))])
        if cur:
            for file in cur:
                with open(file,'r',encoding='utf-8') as f:##注意修改编码如果存储的不是utf-8编码
                    lines=f.readlines()#文件所有行读入lines数组中
                    print(lines)
        else:
            print('你没有选择任何文件')
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月23日
  • 已采纳回答 5月15日
  • 创建了问题 5月15日

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同