【发布时间】:2020-08-13 17:44:34
【问题描述】:
我正在尝试为我的孩子制作一个小型闪存卡程序。到目前为止,这是我想出的,我希望如果我重新分配按钮函数中的变量,它会随机拉出一个新的列表项。但情况似乎并非如此。我知道我的代码不是最漂亮的,我已经自学了大约 3 周的代码,所以如果还有其他任何人可以推荐帮助改进这一点,我将不胜感激。
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()
【问题讨论】: