我必须制作一个代码,该代码将生成一个GUI,显示三个类的测试结果,所有信息都将存储在一个文件中。我试图做代码我可以制作GUI和单选按钮,这一切都有效,但是得到一个按钮,允许你把文本框中的内容按字母顺序排列。
from tkinter import *
root = Tk()
root.title("Test scores")
root.geometry("500x300")
app = Frame(root)
app.grid()
test1 = open("names.txt","r")
test2 = open("score.txt","r")
test3 = open("class.txt","r")
class Application(Frame):
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
Label(self,
text = "Which class "
).grid(row = 0,column = 0, sticky = W)
Label(self,
text = "Select a class"
).grid(row = 1, column = 0, sticky = W)
self.choose = StringVar()
self.choose.set(None)
Radiobutton(self,
text = "Class 1",
variable = self.choose,
value = test1.read(),
command = self.update_text
).grid(row = 2, column = 0, sticky = W)
self.second = BooleanVar()
Radiobutton(self,
text = "Class 2",
variable = self.choose,
value = test2.read(),
command = self.update_text
).grid(row = 3, column = 0, sticky = W)
self.third = BooleanVar()
Radiobutton(self,
text = "Class 3",
variable = self.choose,
value = test3.read(),
command = self.update_text
).grid(row = 4, column = 0, sticky = W)
self.bttn = Button(self)
self.bttn["text"] = "Alphabetical"
self.bttn["command"] = self.update_order
self.bttn.grid(row = 4, column = 1, sticky = W)
self.results_txt = Text(self, width = 40, height = 5, wrap = WORD)
self.results_txt.grid(row = 5, column = 0, columnspan = 3)
def update_text(self):
likes = ""
likes += self.choose.get()
self.results_txt.delete(0.0, END)
self.results_txt.insert(0.0, likes)
def update_order(self):
sortfile = open("sorted1.txt","w")
for line in sorted(self.results_txt, key = str.lower):
sortfile.write(line)
app = Application(root)
root.mainloop()
到目前为止,这是我的代码。
我试图对它进行排序,但它不起作用这是我尝试的代码,因为我在网上查看并看到了这个
def update_order(self):
str(sorted(self.results_txt, key=lambda student: student[2]))
我将如何排序文件行 所以如果第一行有“名字”“第一分”“第二分”“第三分” 然后在第二行“不同的名字”“第一个得分”“第二个得分”“第三个得分” 你将如何按字母顺序按行中的名称对其进行排序。我需要将每个分数放在不同的文件上吗?