简体   繁体   English

Tkinter框架中的滚动条(python)

[英]Scrollbar in Frame of tkinter(python)

I've had a little dilemma and I think that it might be the same for a few people(hopefully). 我遇到了一些难题,我认为对于一些人来说(希望如此)可能是一样的。 The following code gets names from a text file in the same folder, then prints them out in a frame. 以下代码从同一文件夹中的文本文件获取名称,然后将其打印在框架中。 Unfortunately I don't know how to add a scrolling functionality into just this frame (nameframe) . 不幸的是,我不知道如何在此框架(nameframe)中添加滚动功能。 I need this code so that when you have a long list of names, all the names can be seen. 我需要此代码,以便当您有很长的名称列表时,可以看到所有名称。 Currently you can only see half of the names. 目前,您只能看到一半的名称。 I also would like the buttons to be the same size. 我也希望按钮的尺寸相同。

from tkinter import *
import time
import datetime
import re

root = Tk()
root.title("Attendence Register")
root.geometry('1350x650+0+0')

root.resizable(False, False)

nameframe = Frame(root, height=650, width=300)
nameframe.pack(side='left')

saveframe = Frame(root, height=650, width=300)
saveframe.pack(side='right')

outlist = []

def saveDataPresent(line):
    presentcount[line] += 1

    if presentcount[line] %2 == 1:
        present[line].configure(bg='#ff4dd2')
        line = (line + ' is present')
        outlist.append(line)
        #print(outlist)

    else:
        present[line].configure(bg='#66ff66')
        line = (line + ' is present')
        outlist.remove(line)
        #print(outlist)

def saveDataAbsent(line):
    absentcount[line] += 1

    if absentcount[line] % 2 == 1:
        absent[line].configure(bg='#ff4dd2')
        line = (line + ' is absent')
        outlist.append(line)
        #print(outlist)

    else:
        absent[line].configure(bg='#ff6666')
        line = (line + ' is absent')
        outlist.remove(line)
        #print(outlist)

def saveDataIll(line):
    illcount[line] += 1

    if illcount[line] % 2 == 1:
        ill[line].configure(bg='#ff4dd2')
        line = (line + ' is ill')
        outlist.append(line)
        #print(outlist)

    else:
        ill[line].configure(bg='#ffa31a')
        line = (line + ' is ill')
        outlist.remove(line)
        #print(outlist)

def saveDataHoliday(line):
    holidaycount[line] += 1

    if holidaycount[line] % 2 == 1:
        holiday[line].configure(bg='#ff4dd2')
        line = (line + ' is holiday')
        outlist.append(line)
        #print(outlist)

    else:
        holiday[line].configure(bg='light blue')
        line = (line + ' is holiday')
        outlist.remove(line)
        #print(outlist)

def saveData():
    now = datetime.datetime.now()
    now = str(now)
    dire = 'logs/'
    now = dire + now

    now = re.sub(':', '', now)
    now += '.txt'

    log = open(now, "w+")
    log.close()
    log = open(now, "a")
    for i in outlist:
        i = (i + '\n')
        log.write(i)
    log.close()

text = open('names.txt','r')
line = text.readline()
count = 0
present = {}
absent = {}
ill = {}
holiday = {}

presentcount = {}
absentcount = {}
illcount = {}
holidaycount = {}

for line in text:
    count+= 1
    name = Label(nameframe, text=line)
    name.grid(row=count, column = 0)

    presentcount[line] = 0
    absentcount[line] = 0
    illcount[line] = 0
    holidaycount[line] = 0


    present[line] =  Button(nameframe, text='/', pady = 20, padx=20, bg ='#66ff66', command=lambda line=line: saveDataPresent(line))
    present[line].grid(row=count, column = 2)

    holiday[line] = Button(nameframe, text='H', pady=20, padx=20, bg='light blue', command=lambda line=line: saveDataHoliday(line))
    holiday[line].grid(row=count, column=3)

    ill[line] = Button(nameframe, text='ill', pady=20, padx=20, bg ='#ffa31a', command=lambda line=line: saveDataIll(line))
    ill[line].grid(row=count, column=4)

    absent[line] = Button(nameframe, text='NA', pady=20, padx=20, bg ='#ff6666', command=lambda line=line: saveDataAbsent(line))
    absent[line].grid(row=count, column=5)

savebut = Button(saveframe, text='Save', pady = 20, padx=20, command=saveData)
savebut.pack()

root.mainloop()

Thanks for any help I hope my question is clear. 感谢您的帮助,希望我的问题清楚。 In summary, I would like to know how to add a functioning scroll bar, or atleast something to help to be able to see all the names. 总而言之,我想知道如何添加功能正常的滚动条,或者至少要有一些帮助来查看所有名称。 This scroll bar should only affect nameframe . 此滚动条应仅影响nameframe To show my situation more clearly: Image of the frame cutting 为了更清楚地显示我的情况: 框架切割的图像

This is the sort of thing that I'm looking for: 这是我正在寻找的东西:

nameframe = Frame(root, height=650, width=300)
nameframe.pack(side='left')

vsb = Scrollbar(orient="vertical", command=nameframe.yview)
nameframe.configure(yscrollcommand=vsb.set)

saveframe = Frame(root, height=650, width=300)

This commes up with the error: 'Frame' object has no attribute 'yview' saveframe.pack(side='right') 出现以下错误:'Frame'对象没有属性'yview'saveframe.pack(side ='right')

As pointed out in the link I provided you need to be using a canvas to scroll over widgets by adding a frame window to the canvas. 正如我提供的链接中所指出的那样,您需要通过在画布上添加框架窗口来使用画布来滚动小部件。 There is also this post that may better explain what to do here: Adding a scrollbar to a group of widgets in Tkinter . 也有一篇文章可以更好地解释该怎么做: 在Tkinter中将滚动条添加到一组小部件中

My example is only to solve the problem of scrolling through your widgets. 我的示例仅用于解决滚动小部件的问题。 Please note you may have other issues to review. 请注意,您可能还有其他问题需要审查。

I prefer to use grid() manager so I updated your code accordingly. 我更喜欢使用grid()管理器,因此我相应地更新了您的代码。 Please let me know if you have any questions. 请让我知道,如果你有任何问题。

Here is your code updated (with some general cleanup): 这是您的代码更新(进行了一些常规清理):

import tkinter as tk
import datetime
import re

root = tk.Tk()
root.title("Attendence Register")
root.geometry('1350x650+0+0')
root.resizable(False, False)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
saveframe = tk.Frame(root, height=650, width=300)
saveframe.grid(row=0, column=2)

outlist = []

def saveDataPresent(line):
    presentcount[line] += 1
    if presentcount[line] %2 == 1:
        present[line].configure(bg='#ff4dd2')
        line = (line + ' is present')
        outlist.append(line)
    else:
        present[line].configure(bg='#66ff66')
        line = (line + ' is present')
        outlist.remove(line)

def saveDataAbsent(line):
    absentcount[line] += 1
    if absentcount[line] % 2 == 1:
        absent[line].configure(bg='#ff4dd2')
        line = (line + ' is absent')
        outlist.append(line)
    else:
        absent[line].configure(bg='#ff6666')
        line = (line + ' is absent')
        outlist.remove(line)

def saveDataIll(line):
    illcount[line] += 1
    if illcount[line] % 2 == 1:
        ill[line].configure(bg='#ff4dd2')
        line = (line + ' is ill')
        outlist.append(line)
    else:
        ill[line].configure(bg='#ffa31a')
        line = (line + ' is ill')
        outlist.remove(line)

def saveDataHoliday(line):
    holidaycount[line] += 1
    if holidaycount[line] % 2 == 1:
        holiday[line].configure(bg='#ff4dd2')
        line = (line + ' is holiday')
        outlist.append(line)

    else:
        holiday[line].configure(bg='light blue')
        line = (line + ' is holiday')
        outlist.remove(line)

def saveData():
    now = datetime.datetime.now()
    now = str(now)
    dire = 'logs/'
    now = dire + now
    now = re.sub(':', '', now)
    now += '.txt'
    log = open(now, "w+")
    log.close()
    log = open(now, "a")
    for i in outlist:
        i = (i + '\n')
        log.write(i)
    log.close()

text = ['names', 'names', 'names', 'names', 'names', 'names', 'names', 'names', 'names', 'names', 'names', 'names', 'names', 'names', 'names', 'names', 'names', 'names', 'names', 'names']
#line = text.readline()
count = 0
present = {}
absent = {}
ill = {}
holiday = {}
presentcount = {}
absentcount = {}
illcount = {}
holidaycount = {}

canvas = tk.Canvas(root, borderwidth=0)
frm = tk.Frame(canvas)
vsb = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)
canvas.grid(row=0, column=0, sticky="ns")
vsb.grid(row=0, column=1, sticky="ns")
canvas.create_window((4,4), window=frm, anchor="nw")

def onFrameConfigure(canvas):
    canvas.configure(scrollregion=canvas.bbox("all"))

frm.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))

for line in text:
    count += 1

    name = tk.Label(frm, text=line)
    name.grid(row=count, column=0)

    presentcount[line] = 0
    absentcount[line] = 0
    illcount[line] = 0
    holidaycount[line] = 0

    present[line] =  tk.Button(frm, text='/', pady=20, padx=20, bg='#66ff66', command=lambda line=line: saveDataPresent(line))
    present[line].grid(row=count, column=2)
    holiday[line] = tk.Button(frm, text='H', pady=20, padx=20, bg='light blue', command=lambda line=line: saveDataHoliday(line))
    holiday[line].grid(row=count, column=3)
    ill[line] = tk.Button(frm, text='ill', pady=20, padx=20, bg='#ffa31a', command=lambda line=line: saveDataIll(line))
    ill[line].grid(row=count, column=4)
    absent[line] = tk.Button(frm, text='NA', pady=20, padx=20, bg='#ff6666', command=lambda line=line: saveDataAbsent(line))
    absent[line].grid(row=count, column=5)

tk.Button(saveframe, text='Save', pady=20, padx=20, command=saveData).grid(row=0, column=0)

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM