生成一个随机数(不止一次)并在两个类函数中使用它

问题描述 投票:0回答:2

我正在使用 tkinter 构建一个 GUI,其中两个按钮修改两个文本字段。一个按钮从

example_dict
中选择一个随机键并将其显示在
text1
中,另一个按钮在
text2
中显示相应的值。我选择了 OOP 方法来组织我的代码,因为它似乎最容易阅读。

为了给出更简洁的概述,我将脚本归结为我的问题。在实际脚本中,我使用 pandas dataframe 而不是

example_dict
,但出于这个问题的目的,我想保持复制和粘贴的可能性。

import tkinter as tk
from random import randint

example_dict = {'a': "one", 'b': "two", 'c': "three"}

class MainApp:
    def __init__(self, master):
        self.master = master

        self.reveal_button = tk.Button(master, text="reveal", command=self.reveal)
        self.reveal_button.pack()
        self.reveal_button.place(relx=0.5, rely=0.25, anchor=tk.CENTER)
        
        self.random_button = tk.Button(master, text="randomize", command=self.random_word)
        self.random_button.pack()
        self.random_button.place(relx=0.5, rely=0.6, anchor=tk.CENTER)
        
        # create two text fields that will display the key:value pair from the dictionary
        self.text1 = tk.Text(master, height=2, width=20)
        self.text1.pack()
        self.text1.place(relx=0.3, rely=0.25, anchor=tk.CENTER)
        
        self.text2 = tk.Text(master, height=2, width=20)
        self.text2.pack()
        self.text2.place(relx=0.7, rely=0.25, anchor=tk.CENTER)

    def random_word(self):
        rand_num = randint(0,2)
        self.text1.delete(1.0, tk.END)
        self.text2.delete(1.0, tk.END)
        self.text1.insert(tk.END, list(example_dict.keys())[rand_num])

    def reveal(self):
        self.text2.delete(1.0, tk.END)
        self.text2.insert(tk.END, list(example_dict.values())[rand_num])

root = tk.Tk()
MainApp(root)
root.mainloop()

问题当然是

rand_num
仅在
random_word
中定义,当我单击
reveal_button
时导致 NameError。我不确定在哪里生成随机数以便能够在两个类函数中使用相同的数字。

我尝试了一些东西。首先,我创建了一个单独的函数,它只生成一个随机整数,然后我从其他两个函数中调用它。我没想到这会起作用,因为它每次调用时都会生成不同的整数,从而导致键值对不匹配。尝试看起来像这样:

def random_number(self):
    return randint(0,2)

def random_word(self):
    rand_num = self.random_number()
    self.text1.delete(1.0, tk.END)
    self.text2.delete(1.0, tk.END)
    self.text1.insert(tk.END, list(example_dict.keys())[rand_num])

def reveal(self):
    rand_num = self.random_number()
    self.text2.delete(1.0, tk.END)
    self.text2.insert(tk.END, list(example_dict.values())[rand_num])

其次,我将

rand_num
声明为全局变量,这有效,但每次我看到提到全局变量时,人们都说应该避免使用它们,这让我相信我也应该避免使用它们。

def random_word(self):
    global rand_num
    rand_num = randint(0,2)
    self.text1.delete(1.0, tk.END)
    self.text2.delete(1.0, tk.END)
    self.text1.insert(tk.END, list(example_dict.keys())[rand_num])

我的另一个想法是生成随机数作为实例属性,但它应该只生成一次数字,每次单击

random_button
时我都需要它生成一个新数字。

如何在每次单击

random_button
时生成一个随机数,然后我可以将其与我的两个函数一起使用?

python python-3.x tkinter random python-class
2个回答
0
投票

您可以创建一个类字段并以这种方式使用它:

def __init__(self):
    self.random_num = randint(0,2)

def abc(self):
    print(self.random_num )

0
投票

也许你可以在实例中存储最后一个随机值?

class MainApp:
    def __init__(self, master):
        self.master = master
        self.last_random = None
        ...

    def random_word(self):
            if not self.last_random:
                self.last_random = randint(0,2)
            self.text1.delete(1.0, tk.END)
            self.text2.delete(1.0, tk.END)
            self.text1.insert(tk.END, list(example_dict.keys())[self.last_random])

    def reveal(self):
        if not self.last_random:
            self.last_random = randint(0,2)
        self.text2.delete(1.0, tk.END)
        self.text2.insert(tk.END, list(example_dict.values())[self.last_random])
© www.soinside.com 2019 - 2024. All rights reserved.