【发布时间】:2019-08-30 03:30:11
【问题描述】:
我是老师。使用 Tkinter 和 Openpyxl,我需要编写一个图形用户界面,我可以在其中单击“.xlsx”文件中的学生列表。我想用一个复选按钮“链接”每个学生的名字,并在他/她的复选按钮处于活动状态时显示他/她的分数(保存在“.xlsx”文件中)。如何创建一组我以后可以单独引用的小部件/检查按钮?在下面的代码中,每个检查按钮都具有相同的名称!!!
# -*- coding: utf-8 -*-
from tkinter import *
import openpyxl
wb = openpyxl.load_workbook('marks.xlsx', data_only=True)
sheet = wb.active
names_and_rows = {}
for i in range(2, sheet.max_row + 1):
name = sheet.cell(row=i, column=1).value
names_and_rows[name] = i
root = Tk()
root.title("Student's marks")
students_names = Frame(root, bd=1, relief="solid")
students_names.pack(side="left")
student_marks = Frame(root, bd=1, relief="solid")
student_marks.pack(side="right")
message = Label(student_marks, text="You still haven't checked on any student's name")
message.pack()
def get_marks(v):
marks = ""
for i in range(2, sheet.max_column + 1):
information = str(sheet.cell(row=1, column=i).value) + ": " + str(sheet.cell(row=v, column=i).value) + "\n"
marks = marks + information
if (v.get() == 1):
message.config(text=marks)
else:
message.config(text="You still haven't checked on any student's name")
list_of_widgets = []
for k, v in names_and_rows.items():
square = Checkbutton(students_names, variable=v, onvalue=1, offvalue=0, text=k, command=lambda: get_marks(v))
list_of_widgets.append(square)
square.pack()
root.mainloop()
【问题讨论】:
标签: python tkinter widget openpyxl