Tkinter按钮只允许单击一下

时间:2018-10-25 10:05:20

标签: python python-3.x tkinter

因此,我想创建一个窗口,该窗口在单击时将循环显示各个术语,并在单击其他按钮时翻转卡片,并且所有按钮都可以工作,但是该按钮只能单击一次。我单击第一时间,出现下一个单词,但是第二次没有任何反应。两个按钮都一样。

def flashCard():
    global i
    global j
    i=0
    j=0
    word = tmp[0][0]
    flashCard1 = Toplevel()
    flashCard1.title("Flash Cards!")
    flashCard1.geometry("450x200")

    term = Label(flashCard1, text=word, font=("Helvetica", 32, "bold"))
    term.grid(row=0, column=0, padx=150, pady=75, columnspan=2)

    def flip(i,j):
        if j == 0:
            term.configure(text=tmp[i][1])
        elif j == 1:
            term.configure(text=tmp[i][0])

    def nextC(i,j):
        i=i+1
        try:
            term.configure(text=tmp[i][0])
        except:
            messagebox.showinfo("Error", "No more cards - back to beginning")
            i=0
            term.configure(text=tmp[i][0])

    flipBtn = Button(flashCard1, text="See other side", command=lambda: flip(i,j))
    flipBtn.grid(row=1, column=0)

    nextBtn = Button(flashCard1, text="See next card", command=lambda: nextC(i,j))
    nextBtn.grid(row=1, column=1)

谢谢!

2 个答案:

答案 0 :(得分:1)

主要问题是您将ij作为参数传递给回调函数,因此,当递增i时,将递增该本地参数,而 not < / em>具有相同名称的全局变量。删除参数,然后在函数内添加global i。另外,您永远都不会更改j,因此无法将卡翻转回去(万一有此意图,则根本不需要j)。另外,您可以使用nextC代替% len(tmp)来简化Try/except函数。

def flip():
    global j
    j = 0 if j else 1
    term.configure(text=tmp[i][j])

def nextC():
    global i, j
    i, j = (i+1) % len(tmp), 0
    term.configure(text=tmp[i][0])

flipBtn = Button(flashCard1, text="See other side", command=flip)
nextBtn = Button(flashCard1, text="See next card", command=nextC)

答案 1 :(得分:-1)

在函数中递增ij仅在本地递增它们,您可以通过将可变对象传递给列表之类的方法来解决。 Reference。为此,我将ij替换为indices的列表,其余部分与在您需要对indices[0]进行更改时所称的i相同。和indices[1]用于更改j

from tkinter import *
from tkinter import messagebox


def flashCard():
    indices = [0,0]

    flashCard1 = Tk()
    tmp = [[str(y)+str(x) for x in range(2)] for y in range(20)]
    print(tmp)
    word = tmp[indices[0]][indices[1]]
    flashCard1.title("Flash Cards!")
    flashCard1.geometry("450x200")

    term = Label(flashCard1, text=word, font=("Helvetica", 32, "bold"))
    term.grid(row=0, column=0, padx=150, pady=75, columnspan=2)

    def flip(indices):
        if indices[1] == 0:
            indices[1]+=1
            term.configure(text=tmp[indices[0]][indices[1]])
        elif indices[1] == 1:
            indices[1]-=1
            term.configure(text=tmp[indices[0]][indices[1]])

    def nextC(indices):
        indices[0]=indices[0]+1
        try:
            term.configure(text=tmp[indices[0]][0])
        except:
            messagebox.showinfo("Error", "No more cards - back to beginning")
            indices[0]=0
            term.configure(text=tmp[indices[0]][0])

    flipBtn = Button(flashCard1, text="See other side", command=lambda:flip(indices))
    flipBtn.grid(row=1, column=0)

    nextBtn = Button(flashCard1, text="See next card", command=lambda:nextC(indices))
    nextBtn.grid(row=1, column=1)

    mainloop()

flashCard()

出于测试目的,我初始化了tmp。但是无论大小(n,2)

,您都可以拥有它