1

So I want to create a window which will cycle through the terms when clicked, and also flip the card when other button clicked, and its all working, but the button can only click once. I click the fist time, and the next word comes up, but the second time, nothing happens. This is the same for both buttons.

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)

Thanks!

Harry PJ
  • 27
  • 1

2 Answers2

1

The main problem is that you pass i and j as parameters to the callback functions, thus when you increment i, you increment that local parameter, and not the global variable of the same name. Remove the parameters and add global i inside the function. Also, you never change j, thus you can not flip the card back (in case this is intended, you do not need j at all). Also, you could simplify the nextC function by using % len(tmp) instead of 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)
tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

Incrementing i and j in your function only increments them locally, you can go around that by passing a mutable object into a method such as a list. Reference. To do I replaced i and j by a list of indices, the rest is just the same I call indices[0] when you need to perform changes to i and indices[1] for changes to 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()

I initialized tmp for testing purposes. But you can have it whatever it it's size (n,2)

Hadi Farah
  • 1,091
  • 2
  • 9
  • 27
  • 1
    code-only answers aren't very useful, as it requires us to compare your code to the original line-by-line and character-by-character to see what you changed. Your answer would be much better if you added an explanation of what you did and why you did it. – Bryan Oakley Oct 25 '18 at 12:10
  • @BryanOakley I had my explanation in a comment but deleted it and forgot to add it here. I fI do please remove the downvote. – Hadi Farah Oct 26 '18 at 08:08