python小白求助
#引入需要的库from tkinter import *
from unicodedata import name
import tkinter as tk
import os
import tkinter.messagebox as messagebox
#定义大类
class SongInformation:
def __init__(self, song,singer,time):
self.song=song
self.singer=singer
self.time=time
songs=[]
filename="song.txt"
file=open(filename,'r',encoding='utf-8')
iffile:
line=file.readline()
while line!='':
list1=line.split()
if list1:
string1=list1
if string1=='':
continue
if string1!='':
string2=list1
string3=list1
songs.append(SongInformation(string1,string2,string3))
line=file.readline()
file.close()
#制作主页并制作跳转功能
def menu():
top=tk.Tk()
top.title("歌曲信息管理系统")
top.geometry('280x280')
btn1=tk.Button(top,text="录入歌曲信息",
command=lambda:)
btn2=tk.Button(top,text="删除歌曲信息",
command=lambda:)
btn3=tk.Button(top,text="根据歌手分组",
command=lambda:)
btn4=tk.Button(top,text="浏览歌曲信息",
command=lambda:)
btn5=tk.Button(top,text="查询歌曲信息",
command=lambda:)
btn6=tk.Button(top,text="退出管理系统",
command=top.destroy)
btn1.pack()
btn2.pack()
btn3.pack()
btn4.pack()
btn5.pack()
btn6.pack()
top.mainloop()
#跳转界面
#录入界面设置
def insert():
insert=tk.Tk()
insert.title('录入歌曲界面')
label1=tk.Label(insert,text="输入歌曲名称:")
label1.grid(row=1,column=1)
song1=tk.StringVar()
song2=tk.Entry(insert,textvariable=song1)
song2.grid(row=1,column=2)
label2=tk.Label(insert,text="输入歌手名称:")
label2.grid(row=2,column=1)
singer1=tk.StringVar()
singer2=tk.Entry(insert,textvariable=singer1)
singer2.grid(row=2,column=2)
label3=tk.Label(insert,text="输入发行年月:")
label3.grid(row=3,column=1)
time1=tk.StringVar()
time2=tk.Entry(insert,textvariable=time1)
time2.grid(row=3,column=2)
def insert1():
song=song2.get()
a=str(song)
singer=singer2.get()
b=str(singer)
time=time2.get()
c=str(time)
songs.append(SongInformation(a,b,c))
if len(a)!=0 and len(b)!=0 and len(c)!=0:
with open('song.txt', 'a',encoding='utf-8') as f:
for item in songs:
f.write(str(item.song)+" "+str(item.singer)
+" "+str(item.time)+'\n')
insuc()
else:
warn=tk.Tk()
warn.title("错误")
label1=tk.Label(warn,text="请重新输入!")
label1.pack()
warn.mainloop()
btn1=tk.Button(insert,text="录入",
command=insert1)
btn1.grid(row=4,column=1)
btn2=tk.Button(insert,text="结束",
command=insert.destroy)
btn2.grid(row=4,column=2)
insert.mainloop()
#录入成功的弹窗
def insuc():
insuc=tk.Tk()
insuc.title('录入成功')
label1=tk.Label(insuc,text="录入成功!")
label1.pack()
insuc.mainloop()
#删除歌曲界面
def dele():
dele=tk.Tk()
dele.title('删除歌曲界面')
label1=tk.Label(dele,text='输入要删除的歌曲名称:')
label1.grid(row=1,column=1)
dele1=tk.StringVar()
dele1=tk.Entry(dele,textvariable=dele1)
dele1.grid(row=1,column=2)
dele2=dele1.get()
dele3=str(dele2)
def dete(keyword):
keyword=dele3
for index,item in enumerate(songs):
if songs.song==keyword:
x=index
del songs
file=open(filename,'w+',encoding='utf-8')
for item in songs:
file.write(str(item.song)+""+str(item.singer)
+""+str(item.time)+'\n')
file.close()
btn1=tk.Button(dele,text="确定删除",
command=dete())
btn1.grid(row=2,column=1)
btn2=tk.Button(dele,text="结束",
command=insert.destroy)
btn2.grid(row=4,column=2)
dele.mainloop()
这个是歌曲管理系统的一部分程序,删除歌曲这个要么是出错要么删不了,不知道该怎么继续做了(我知道自己写的很乱.. 确实乱。
这样试试
def dete(keyword=''):
keyword=dele3
for index,item in enumerate(songs):
if songs.song==keyword:
x=index
break
del songs
with open(filename,'w+',encoding='utf-8') as f:
for item in songs:
f.write(str(item.song)+""+str(item.singer)
+""+str(item.time)+'\n')
btn1=tk.Button(dele,text="确定删除",
command=dete) ba21 发表于 2022-4-13 18:12
确实乱。
这样试试
in detedel songs
UnboundLocalError: local variable 'x' referenced before assignment
还是报错{:10_266:} UNLsian 发表于 2022-4-13 18:50
in detedel songs
UnboundLocalError: local variable 'x' referenced before assignment
还是报 ...
def dete(keyword=''):
keyword=dele3
for index,item in enumerate(songs[:]):
if songs.song==keyword:
del songs
break
with open(filename,'w+',encoding='utf-8') as f:
for item in songs:
f.write(str(item.song)+""+str(item.singer)
+""+str(item.time)+'\n') ba21 发表于 2022-4-13 20:20
def dele():
dele=tk.Tk()
dele.title('删除歌曲界面')
label1=tk.Label(dele,text='输入要删除的歌曲名称:')
label1.grid(row=1,column=1)
dele1=tk.StringVar()
dele2=tk.Entry(dele,textvariable=dele1)
dele2.grid(row=1,column=2)
dele2=dele1.get()
dele3=str(dele2)
def dete(keyword=''):
keyword=dele3
for index,item in enumerate(songs[:]):
if songs.song==keyword:
del songs
break
with open(filename,'w+',encoding='utf-8') as f:
for item in songs:
f.write(str(item.song)+" "+str(item.singer)
+" "+str(item.time)+'\n')
desuc()
btn1=tk.Button(dele,text="确定删除",
command=dete)
btn1.grid(row=2,column=1)
btn2=tk.Button(dele,text="结束",
command=dele.destroy)
btn2.grid(row=2,column=2)
dele.mainloop()
是要这样改吗,我还是删除不了 UNLsian 发表于 2022-4-13 20:50
是要这样改吗,我还是删除不了
把代码打包上传,截图问题 所在。 ba21 发表于 2022-4-13 21:09
把代码打包上传,截图问题 所在。
https://pan.baidu.com/s/1-_r-YFw7r7rVyaMHDOK20w?pwd=wc88
https://thumbnail1.baidupcs.com/thumbnail/909beb548jf992ffd5cfb8e838e83bb8?fid=1101798322687-250528-885285091714140&rt=pr&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-v%2bmGlTVHXcQYvyPzY2%2fV21VE8oA%3d&expires=8h&chkbd=0&chkv=0&dp-logid=80457249976710787&dp-callid=0&time=1649854800&size=c1536_u864&quality=90&vuk=1101798322687&ft=image&autopolicy=1 #引入需要的库
from tkinter import *
from unicodedata import name
import tkinter as tk
import os
import tkinter.messagebox as messagebox
#定义大类
class SongInformation:
def __init__(self, song,singer,time):
self.song=song
self.singer=singer
self.time=time
songs=[]
filename="song.txt"
file=open(filename,'r',encoding='utf-8')
iffile:
line=file.readline()
while line!='':
list1=line.split()
if list1:
string1=list1
if string1=='':
continue
if string1!='':
string2=list1
string3=list1
songs.append(SongInformation(string1,string2,string3))
line=file.readline()
file.close()
#制作主页并制作跳转功能
def menu():
top=tk.Tk()
top.title("歌曲信息管理系统")
top.geometry('280x280')
btn1=tk.Button(top,text="录入歌曲信息",
command=lambda:)
btn2=tk.Button(top,text="删除歌曲信息",
command=lambda:)
btn3=tk.Button(top,text="根据歌手分组",
command=lambda:)
btn4=tk.Button(top,text="浏览歌曲信息",
command=lambda:)
btn5=tk.Button(top,text="查询歌曲信息",
command=lambda:)
btn6=tk.Button(top,text="退出管理系统",
command=top.destroy)
btn1.pack()
btn2.pack()
btn3.pack()
btn4.pack()
btn5.pack()
btn6.pack()
top.mainloop()
#跳转界面
#录入界面设置
def insert():
insert=tk.Tk()
insert.title('录入歌曲界面')
label1=tk.Label(insert,text="输入歌曲名称:")
label1.grid(row=1,column=1)
song1=tk.StringVar()
song2=tk.Entry(insert,textvariable=song1)
song2.grid(row=1,column=2)
label2=tk.Label(insert,text="输入歌手名称:")
label2.grid(row=2,column=1)
singer1=tk.StringVar()
singer2=tk.Entry(insert,textvariable=singer1)
singer2.grid(row=2,column=2)
label3=tk.Label(insert,text="输入发行年月:")
label3.grid(row=3,column=1)
time1=tk.StringVar()
time2=tk.Entry(insert,textvariable=time1)
time2.grid(row=3,column=2)
def insert1():
song=song2.get()
a=str(song)
singer=singer2.get()
b=str(singer)
time=time2.get()
c=str(time)
songs.append(SongInformation(a,b,c))
if len(a)!=0 and len(b)!=0 and len(c)!=0:
with open('song.txt', 'a',encoding='utf-8') as f:
for item in songs:
f.write(str(item.song)+" "+str(item.singer)
+" "+str(item.time)+'\n')
insuc()
else:
warn=tk.Tk()
warn.title("错误")
label1=tk.Label(warn,text="请重新输入!")
label1.pack()
warn.mainloop()
btn1=tk.Button(insert,text="录入",
command=insert1)
btn1.grid(row=4,column=1)
btn2=tk.Button(insert,text="结束",
command=insert.destroy)
btn2.grid(row=4,column=2)
insert.mainloop()
#录入成功的弹窗
def insuc():
insuc=tk.Tk()
insuc.title('录入成功')
label1=tk.Label(insuc,text="录入成功!")
label1.pack()
insuc.mainloop()
#删除歌曲界面
def dele():
dele=tk.Tk()
dele.title('删除歌曲界面')
label1=tk.Label(dele,text='输入要删除的歌曲名称:')
label1.grid(row=1,column=1)
dele1=tk.StringVar()
dele1=tk.Entry(dele,textvariable=dele1)
dele1.grid(row=1,column=2)
# dele2=dele1.get() 应该是点击删除的时候才获取文本
# dele3=str(dele2) get() 得到的本来就是字符串
def dete(): # 不要关键字参数
# keyword=dele3 改成 keyword=dele1.get()
keyword=dele1.get()
result = [] # 加个 result = []
for index,item in enumerate(songs):
if songs.song!=keyword: # == 改成 !=
# x=index 不要
result.append(songs) # 使用 result 列表存放删除后的结果
# del songs 这一行不要
file=open(filename,'w+',encoding='utf-8')
for item in result: # songs 改成 result
file.write(str(item.song)+""+str(item.singer)
+""+str(item.time)+'\n')
file.close()
btn1=tk.Button(dele,text="确定删除",
command=dete) # 这里不要 (),() 是调用的得到函数返回值,command 传入函数名即可
btn1.grid(row=2,column=1)
btn2=tk.Button(dele,text="结束",
command=dele.destroy) # 这里应该是关闭删除界面,改成 dele.destroy
btn2.grid(row=4,column=2)
dele.mainloop()
menu() isdkz 发表于 2022-4-13 21:57
成功了!!谢谢你!!!!!!!!!!!!!!!!{:10_266:}{:10_266:}{:10_250:} UNLsian 发表于 2022-4-13 22:05
成功了!!谢谢你!!!!!!!!!!!!!!!!
不客气,给个最佳答案呗{:5_109:}
页:
[1]