按钮“开始”以另外五个按钮结束,每个按钮一个。 Tkinter

时间:2018-09-16 02:32:40

标签: python tkinter

所以我一直在做一个倒数计时器(有一点复制,但是我有点在学习Tkinter这样),所以我为按钮使用了for循环,当我为“ START”做它时,它就分开了分成5个按钮。每个START字母一个。我不明白为什么会这样,请有人帮我。

from tkinter import *
import tkinter.messagebox
import time

def iTimer(source, side):
    keepthing = Frame(source, borderwidth=4, bd=4, bg="sky blue")
    keepthing.pack(side=side, expand=YES, fill=BOTH)
    return keepthing

def boutonia(source, side, text, command=None):
    keepthing = Button(source, text=text, command=command)
    keepthing.pack(side=side, expand=YES, fill=BOTH)
    return keepthing

class app(Frame):
    def __init__(self):

        def funk(self):
           pass

        Frame.__init__(self)
        self.option_add('*Font', 'arial 20 bold')
        self.pack(expand=YES, fill=BOTH)
        self.master.title('Countdown timer')

        display = StringVar()
        Entry(self, relief=SUNKEN,
            textvariable=display,justify='right',bd=30,bg="sky blue").pack(side=TOP, expand=YES,fill=BOTH)

        for clearBut in(["CE"],["C"]):
            erase = iTimer(self, TOP)
            for ichar in clearBut:
                boutonia(erase, LEFT, ichar, lambda keepthing=display, q=ichar: keepthing.set(''))

        for Numrow in ("789", "456", "123", "0"):
            FunctionNum = iTimer(self, TOP)
            for iEquals in Numrow:
                boutonia(FunctionNum, LEFT, iEquals, lambda keepthing=display, q=iEquals: keepthing.set(keepthing.get() + q))

        # this is where the problem seems to be
        for start in(["START"]):
            count = iTimer(self, TOP)
            for ichar in start:
                boutonia(count, LEFT, ichar, lambda keepthing=display, q=ichar: keepthing.set(''))

if __name__ == '__main__':
    app().mainloop()

1 个答案:

答案 0 :(得分:0)

我认为问题出在嵌套的for循环内。它会针对“ START”中的每个字符进行迭代(因此迭代了五次)。

for start in(["START"]):
# This code will execute once.
    for ichar in start:
        # This code will execute five times. Once for each character in "START"

尝试删除第二个for循环,看看是否可行。