簡體   English   中英

Tkinter框架中的滾動條(python)

[英]Scrollbar in Frame of tkinter(python)

我遇到了一些難題,我認為對於一些人來說(希望如此)可能是一樣的。 以下代碼從同一文件夾中的文本文件獲取名稱,然后將其打印在框架中。 不幸的是,我不知道如何在此框架(nameframe)中添加滾動功能。 我需要此代碼,以便當您有很長的名稱列表時,可以看到所有名稱。 目前,您只能看到一半的名稱。 我也希望按鈕的尺寸相同。

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()

感謝您的幫助,希望我的問題清楚。 總而言之,我想知道如何添加功能正常的滾動條,或者至少要有一些幫助來查看所有名稱。 此滾動條應僅影響nameframe 為了更清楚地顯示我的情況: 框架切割的圖像

這是我正在尋找的東西:

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)

出現以下錯誤:'Frame'對象沒有屬性'yview'saveframe.pack(side ='right')

正如我提供的鏈接中所指出的那樣,您需要通過在畫布上添加框架窗口來使用畫布來滾動小部件。 也有一篇文章可以更好地解釋該怎么做: 在Tkinter中將滾動條添加到一組小部件中

我的示例僅用於解決滾動小部件的問題。 請注意,您可能還有其他問題需要審查。

我更喜歡使用grid()管理器,因此我相應地更新了您的代碼。 請讓我知道,如果你有任何問題。

這是您的代碼更新(進行了一些常規清理):

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