我有两个问题与此附加代码有关。此代码是我的项目的一部分,我必须管理50名(或更多)学生的出勤率
当您运行这段代码时,您将看到标签框(即考勤框)内有一个额外的空白(可能是画布)。我所想要的是,不应该有额外的空白,滚动条,而不是在最右边,应该在标签结束的地方。 我已经找到了我问题的答案,并看到了一个类似的案例。但在那里,人们希望框架扩展到画布大小。链接(Tkinter: How to get frame in canvas window to expand to the size of the canvas?)。 但在我的例子中,我希望画布的大小等于框架的大小(尽管框架位于画布内部)
我想要的另一件事是,所有复选框最初都应该被选中(显示当前状态),当我取消选中随机复选框(标记缺席),并单击“提交”按钮(尚未在窗口底部创建)时,我应该得到一个列表,第一个元素是“输入日期”,其他元素是卷号,即2018-MC-XX。例如:['01/08/2020','2018-MC-7','2018-MC-11','2018-MC-23','2018-MC-44']。 实际上,我的计划是当我得到一个列表时,我会很容易地将它写入一个文本文件
from tkinter import *
from tkcalendar import DateEntry
root = Tk()
root.geometry('920x600+270+50')
root.minsize(920,600)
Attendance_frame = Frame(root) ### Consider it a Main Frame
Attendance_frame.pack()
attendaceBox = LabelFrame(Attendance_frame, text = 'Take Attendance', bd = 4, relief = GROOVE, labelanchor = 'n',font = 'Arial 10 bold', fg = 'navy blue', width = 850, height = 525) # A Label Frame inside the main frame
attendaceBox.pack_propagate(0)
attendaceBox.pack(pady = 15)
dateFrame = Frame(attendaceBox) # A small frame to accommodate date entry label & entry box
dateFrame.pack(anchor = 'w')
font = 'TkDefaultFont 10 bold'
date_label = Label(dateFrame, text = 'Enter Date : ', font = font).grid(row = 0, column = 0, sticky = 'w', padx = 10, pady = 10)
date_entry = DateEntry(dateFrame, date_pattern = 'dd/mm/yyyy', showweeknumbers = FALSE, showothermonthdays = FALSE)
date_entry.grid(row = 0, column = 1, sticky = 'w')
noteLabel = Label(attendaceBox, text = 'Note: Uncheck the boxes for absentees').pack(anchor = 'w', padx = 10, pady = 5)
canvas = Canvas(attendaceBox, borderwidth=0, background="#ffffff")
checkFrame = Frame(canvas, width = 100, height = 50)
vsb = Scrollbar(canvas, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.pack_propagate(0)
canvas.create_window((4,4), window=checkFrame, anchor="nw")
def onFrameConfigure(canvas):
'''Reset the scroll region to encompass the inner frame'''
canvas.configure(scrollregion=canvas.bbox("all"))
checkFrame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))
for i in range(0,51): # A loop to create Labels of students roll numbers & names
c = Checkbutton(checkFrame, text = f"{'2018-MC-'+str(i+1)} Student {i+1}")
c.grid(row = i, column = 0, padx = 10, sticky = 'w')
mainloop()
所有问题的答案如下:
你应该试试这个
获取选定值
如果要创建项目的垂直列表,则不需要在画布中使用框架。内部框架增加了一些不必要的复杂性。相反,使用
create_window
直接在画布上创建复选按钮您还需要配置
scrollregion
属性,以便滚动条知道要滚动多少虚拟画布最后,要选择它们,您应该为每个checkbutton分配一个变量,并确保该值是正确的值。默认情况下,复选按钮使用值0和1,因此将变量设置为1将使其处于选中状态
相关问题 更多 >
编程相关推荐