Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python更新从列表中随机抽取的变量?_Python_List_Random - Fatal编程技术网

如何使用python更新从列表中随机抽取的变量?

如何使用python更新从列表中随机抽取的变量?,python,list,random,Python,List,Random,我正在尝试为我的孩子们制作一个小的闪存卡程序。这是我到目前为止所想到的,我希望如果我重新分配button函数中的变量,它会随机抽取一个新的列表项。但事实似乎并非如此。我知道我的代码不是最漂亮的,我已经自学了三个星期了,所以如果有任何其他人可以推荐来帮助改进的话,我将不胜感激 import tkinter as tk import random window = tk.Tk() window.geometry("300x600") window.title("Flas

我正在尝试为我的孩子们制作一个小的闪存卡程序。这是我到目前为止所想到的,我希望如果我重新分配button函数中的变量,它会随机抽取一个新的列表项。但事实似乎并非如此。我知道我的代码不是最漂亮的,我已经自学了三个星期了,所以如果有任何其他人可以推荐来帮助改进的话,我将不胜感激

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

有两个问题使代码无法工作,因此我在下面列出了它们

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

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

将tkinter作为tk导入
随机输入
window=tk.tk()
窗口几何(“300x600”)
窗口标题(“闪存卡主控”)
x=[1,2,3,4,5,6,7,8,9]
y=[1,2,3,4,5,6,7,8,9]
a=''
加号='+'
rx=随机选择(x)
ry=随机。选择(y)
随机变量=[rx,ry]
top=tk.标签(text=rx,font=“格鲁吉亚100加粗”)
operator=tk.Label(text=plus,font=“100 bold”)
底部=tk.标签(text=ry,font=“格鲁吉亚100加粗”)
斜杠=tk.Label(text=''.'.'.'.'.'.'.'.''.'.''.'.''.''.''.''.''.'字体=“格鲁吉亚10粗体”)
Answare=tk.Label(text='',font=“格鲁吉亚100黑体”)
顶部网格(行=0,列=1)
运算符.grid(行=1,列=0)
底部网格(行=1,列=1)
slash.grid(行=2,列=0,列span=3)
Answare.grid(行=3,列=1)
def press_one():
answere.config(text=random\u vars[0]+random\u vars[1])
b1=tk.按钮(text=“单击我!”,command=press\u one)
b1.grid()
def press_two():
answere.config(text=a)
随机变量[0]=随机选择(x)
随机变量[1]=随机选择(y)
top.config(text=random\u vars[0],font=“Georgia 100 bold”)
operator.config(text=plus,font=“100 bold”)
bottom.config(text=random\u vars[1],font=“Georgia 100 bold”)
b1=tk.按钮(text=“Next”,command=按两次)
b1.grid()
window.mainloop()

有两个问题使代码无法工作,因此我在下面列出了它们

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

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

将tkinter作为tk导入
随机输入
window=tk.tk()
窗口几何(“300x600”)
窗口标题(“闪存卡主控”)
x=[1,2,3,4,5,6,7,8,9]
y=[1,2,3,4,5,6,7,8,9]
a=''
加号='+'
rx=随机选择(x)
ry=随机。选择(y)
随机变量=[rx,ry]
top=tk.标签(text=rx,font=“格鲁吉亚100加粗”)
operator=tk.Label(text=plus,font=“100 bold”)
底部=tk.标签(text=ry,font=“格鲁吉亚100加粗”)
斜杠=tk.Label(text=''.'.'.'.'.'.'.'.''.'.''.'.''.''.''.''.''.'字体=“格鲁吉亚10粗体”)
Answare=tk.Label(text='',font=“格鲁吉亚100黑体”)
顶部网格(行=0,列=1)
运算符.grid(行=1,列=0)
底部网格(行=1,列=1)
slash.grid(行=2,列=0,列span=3)
Answare.grid(行=3,列=1)
def press_one():
answere.config(text=random\u vars[0]+random\u vars[1])
b1=tk.按钮(text=“单击我!”,command=press\u one)
b1.grid()
def press_two():
answere.config(text=a)
随机变量[0]=随机选择(x)
随机变量[1]=随机选择(y)
top.config(text=random\u vars[0],font=“Georgia 100 bold”)
operator.config(text=plus,font=“100 bold”)
bottom.config(text=random\u vars[1],font=“Georgia 100 bold”)
b1=tk.按钮(text=“Next”,command=按两次)
b1.grid()
window.mainloop()

以下是一个工作版本:

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”的“local”,它们不是同一个变量。您需要学习Python的作用域规则,一旦习惯了这些规则,这些规则就相当简单了


函数“press”中的global语句通知Python您正在引用同一个已经定义的变量。这就解决了问题。

这里有一个工作版本:

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”的变量