【问题标题】:Toggling variables from for loop in python and tkinter在 python 和 tkinter 中从 for 循环切换变量
【发布时间】:2018-11-09 20:19:27
【问题描述】:

我是 python 和堆栈溢出的新手,如果这个问题低于平均水平,我很抱歉。无论如何,我正在尝试用 python 和 tkinter 制作一个注册软件,我想让按钮在紫色之间切换:#ff4dd2。这很难,因为按钮是从循环创建的,我不能为按钮分配变量。如果您能花时间看一下,将不胜感激:)(当前代码按预期工作,希望您能理解我的意思)

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):
    present[line].configure(bg='#ff4dd2')
    line = (line + ' is present')
    outlist.append(line)
    #print(outlist)

def saveDataAbsent(line):
    absent[line].configure(bg='#ff4dd2')
    line = (line + ' is absent')
    outlist.append(line)
    #print(outlist)

def saveDataIll(line):
    ill[line].configure(bg='#ff4dd2')
    line = (line + ' is ill')
    outlist.append(line)
    #print(outlist)

def saveDataHoliday(line):
    holiday[line].configure(bg='#ff4dd2')
    line = (line + ' is on holiday')
    outlist.append(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 = {}

for line in text:
    count+= 1
    name = Label(nameframe, text=line)
    name.grid(row=count, column = 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()

【问题讨论】:

  • 按钮被放入循环上方的字典中。你不能只从字典中获取按钮吗?
  • 请为您的问题添加更多上下文。我不是 100% 确定你需要什么。按钮确实变成紫色。你想让他们改回来吗?
  • 感谢您的意见,感谢您,我已经设法解决了这个问题。我没有使用按钮所在的字典。但是我为每个按钮制作了一个新字典,然后我将其链接到该行,并且有效: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)
  • 感谢 mike 的帮助,并为缺少上下文而感到抱歉。我已经发布了解决方案,因此希望可以消除您对我的问题的任何困惑。我基本上希望他们在变为紫色和按钮颜色之间切换,当按钮为紫色时,它会将某些内容附加到列表中,当它不是紫色时它会删除它

标签: python-3.x function variables tkinter lambda


【解决方案1】:

我把它放在了 cmets 中,但看起来不太好:

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)enter code here

holidaycount 之前被定义为字典:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2013-06-12
    • 2014-05-05
    • 1970-01-01
    • 2022-11-11
    • 2015-02-09
    • 1970-01-01
    相关资源
    最近更新 更多