0

I am trying to make a small flash card program for my kids. Here is what I have come up with so far, I was hoping if I reassigned the variables in the button function it would randomly pull a new list item. But that does not seem to be the case. I know my code is not he prettiest, I’ve been teaching myself how to code for about 3 weeks now, so if there is anything else anyone can recommend to help improve this I would appreciate it.

import tkinter as tk
import random
window = tk.Tk()
window.geometry("300x600")
window.title("Flash Card Master")

x = [1, 2, 3, 4, 5, 6, 7, 8, 9 ];
y = [1, 2, 3, 4, 5, 6, 7, 8, 9 ];
a = ''
plus = '+'
rx = random.choice(x)
ry = random.choice(y)

top = tk.Label(text = rx, font="Georgia 100 bold")
operator = tk.Label(text = plus, font="Georgia 100 bold")
bottom = tk.Label(text = ry, font="Georgia 100 bold")
slash = tk.Label(text = '    ____________________', font="Georgia 10 bold")
answere = tk.Label(text = '', font="Georgia 100 bold")

top.grid(row=0, column=1)
operator.grid(row=1, column=0)
bottom.grid(row=1, column=1)
slash.grid(row=2, column=0, columnspan=3)
answere.grid(row=3, column=1)

def press():
    answere.config(text = rx + ry)
b1 = tk.Button(text = "Click ME!", command = press)
b1.grid()

def press():
    answere.config(text = a)
    rx = random.choice(x)
    ry = random.choice(y)
    top = tk.Label(text = rx, font="Georgia 100 bold")
    operator = tk.Label(text = plus, font="Georgia 100 bold")
    bottom = tk.Label(text = ry, font="Georgia 100 bold")
b1 = tk.Button(text = "Next", command = press)
b1.grid()

window.mainloop()

2 Answers 2

1

There were two problems that were stopping your code from working, so I've listed them below.

Firstly, your 'rx' and 'ry' variables inside your "press" function for generating a new flash card were local, which means they only worked inside the function and didn't change the values outside of the function. I put the 'rx' and 'ry' variables you created at the top inside an array so you can alter them inside a function and have the changes stay.

Secondly, there was a small error inside your second "press" function where you didn't use "config()" on "top", "operator", or "bottom". I fixed that.

import tkinter as tk
import random
window = tk.Tk()
window.geometry("300x600")
window.title("Flash Card Master")

x = [1, 2, 3, 4, 5, 6, 7, 8, 9 ]
y = [1, 2, 3, 4, 5, 6, 7, 8, 9 ]
a = ''
plus = '+'
rx = random.choice(x)
ry = random.choice(y)
random_vars = [rx, ry]

top = tk.Label(text = rx, font="Georgia 100 bold")
operator = tk.Label(text = plus, font="Georgia 100 bold")
bottom = tk.Label(text = ry, font="Georgia 100 bold")
slash = tk.Label(text = '    ____________________', font="Georgia 10 bold")
answere = tk.Label(text = '', font="Georgia 100 bold")

top.grid(row=0, column=1)
operator.grid(row=1, column=0)
bottom.grid(row=1, column=1)
slash.grid(row=2, column=0, columnspan=3)
answere.grid(row=3, column=1)


def press_one():
    answere.config(text = random_vars[0] + random_vars[1])


b1 = tk.Button(text = "Click ME!", command = press_one)
b1.grid()


def press_two():
    answere.config(text = a)
    random_vars[0] = random.choice(x)
    random_vars[1] = random.choice(y)
    top.config(text = random_vars[0], font="Georgia 100 bold")
    operator.config(text = plus, font="Georgia 100 bold")
    bottom.config(text = random_vars[1], font="Georgia 100 bold")


b1 = tk.Button(text = "Next", command = press_two)
b1.grid()

window.mainloop()
Sign up to request clarification or add additional context in comments.

2 Comments

That worked perfectly! Thanks for the help, makes sense when I see it the way you wrote it.
It would look more minimal if you could remove the part of the answer that is not related to the answer :D
1

Here is a working version:

import tkinter as tk
import random
window = tk.Tk()
window.geometry("300x600")
window.title("Flash Card Master")

x = [1, 2, 3, 4, 5, 6, 7, 8, 9 ];
y = [1, 2, 3, 4, 5, 6, 7, 8, 9 ];
a = ''
plus = '+'
rx = random.choice(x)
ry = random.choice(y)

top = tk.Label(text = rx, font="Georgia 100 bold")
operator = tk.Label(text = plus, font="Georgia 100 bold")
bottom = tk.Label(text = ry, font="Georgia 100 bold")
slash = tk.Label(text = '    ____________________', font="Georgia 10 bold")
answere = tk.Label(text = '', font="Georgia 100 bold")

top.grid(row=0, column=1)
operator.grid(row=1, column=0)
bottom.grid(row=1, column=1)
slash.grid(row=2, column=0, columnspan=3)
answere.grid(row=3, column=1)

def press():
    answere.config(text = rx + ry)
b1 = tk.Button(text = "Click ME!", command = press)
b1.grid()

def press():
    global rx, ry
    answere.config(text = a)
    rx = random.choice(x)
    ry = random.choice(y)
    top.config(text = rx)
    operator.config(text = plus)
    bottom.config(text = ry)
b1 = tk.Button(text = "Next", command = press)
b1.grid()

window.mainloop()

You had two problems. First, the line of code rx = tk.Label(...) creates a new instance of Label. In other words, it makes an entirely new widget rather than modifying the old one. Since you never gridded this new widget it never appeared on the screen. Strangely, you had already solved this exact problem when you wrote the line answere.config(text=...). That's how you change the text of an already existing widget.

Second, when you write a line of code like rx = ..., Python creates a new object and assigns it to a variable named "rx." Your original program has two variables with this name: one is "global" - created by an un-indented line of code. The other is "local" to the function "press." They aren't the same variable. You need to learn Python's scoping rules, which are pretty simple once you get used to them.

The global statement in the function "press" informs Python that you are referring to the same variable, already defined. That solves the problem.

1 Comment

That works great! Thanks for the answer and the explanation. Looks like I will be digging into scope rules today!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.