【问题标题】:How do I update a variable that is pulling randomly from a list using python?如何更新使用 python 从列表中随机提取的变量?
【发布时间】: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()

【问题讨论】:

    标签: python list random


    【解决方案1】:

    有两个问题导致您的代码无法正常工作,因此我在下面列出了它们。

    首先,“press”函数中用于生成新闪存卡的“rx”和“ry”变量是本地变量,这意味着它们仅在函数内部起作用,而不会更改函数外部的值。我将您创建的 'rx' 和 'ry' 变量放在数组的顶部,以便您可以在函数中更改它们并保留更改。

    其次,在您的第二个“press”函数中存在一个小错误,您没有在“top”、“operator”或“bottom”上使用“config()”。我修好了。

    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()
    

    【讨论】:

    • 效果很好!感谢您的帮助,当我按照您编写的方式看到它时才有意义。
    • 如果您可以删除与答案无关的部分答案,它看起来会更小:D
    【解决方案2】:

    这是一个工作版本:

    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()
    

    你有两个问题。首先,代码行rx = tk.Label(...) 创建了一个新的Label 实例。换句话说,它制作了一个全新的小部件,而不是修改旧的小部件。由于您从未对这个新小部件进行网格化,因此它从未出现在屏幕上。奇怪的是,当您编写answere.config(text=...) 行时,您已经解决了这个确切的问题。这就是您更改现有小部件文本的方式。

    其次,当您编写像 rx = ... 这样的代码行时,Python 会创建一个新对象并将其分配给名为“rx”的变量。您的原始程序有两个具有此名称的变量:一个是“全局” - 由未缩进的代码行创建。另一个是函数“press”的“本地”。它们不是同一个变量。你需要学习 Python 的作用域规则,一旦你习惯了它们就很简单了。

    函数“press”中的全局​​语句通知 Python 您指的是同一个变量,已定义。这样就解决了问题。

    【讨论】:

    • 效果很好!感谢您的回答和解释。看来我今天要深入研究范围规则了!
    猜你喜欢
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多