如何在不同 class 的共享数据变量中设置随机数

How do I set a random number within a shared data variable in different class

我正在尝试获取在文本输入框中生成的随机数。它不起作用,因为我不确定如何共享变量。如果有帮助,我在 visual studio 代码中使用 python tkinter。变量 receipt_no 应该在文本输入框中生成一个介于 1000-9999 之间的随机数。我得到一个错误 'str' object has no attribute 'set'.

import tkinter as tk
import random

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
  
        self.shared_data = {"receipt_no":tk.IntVar,
                            "receipt.no":tk.IntVar,
                            "receipt.no".set(str(random.randint(1000,9999))):tk.IntVar}

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage,RandomPage):
                  
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")
    def show_frame(self, page_name):
        frame = self.frames[page_name]
        '''Show a frame for the given page name'''

        frame.tkraise()       

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, bg='#3d3d5c')
        self.controller = controller

        def next():
           controller.show_frame('RandomPage')

        next_button = tk.Button(text='Next',
                                command=next,
                                relief='raised',
                                borderwidth=3,
                                width=18,
                                height=2)
        next_button.place(x=1090, y=475)                  

class RandomPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, bg='#3d3d5c')
        self.controller = controller

        order_number = tk.Label(self,
                         text="Customer order number",
                         font=('arial', 20, 'bold'),
                         foreground='#ffffff',
                         background='#33334d')
        order_number.place(anchor=tk.N, bordermode=tk.INSIDE, x=200, y=500) 
                 
        order_number_entry =tk.Entry(self,
                                      width=12,
                                      font=('Arial 20'),
                                      textvariable=self.controller.shared_data["receipt_no"])  
        order_number_entry.place(anchor=tk.N, bordermode=tk.INSIDE, x=500, y=500)  

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

我已尝试设置该值并尝试将其分享给其他 class。我希望得到一个随机生成的数字,介于 1000-9999 之间。

由于您正在创建字典以获取 receipt_no 值来设置 Entry 小部件的文本,因此不要使用 .set 生成其值,只需创建一个 tk.IntVar() 变量和一个string 存储随机数的变量。

self.shared_data = {"receipt_no":tk.IntVar(),
                    "random_receipt_no":str(random.randint(1000,9999))}

现在,要设置 Entry 小部件的文本,您可能需要使用 insert 方法:

order_number_entry =tk.Entry(self,
                             width=12,
                             font=('Arial 20'), 
                             textvariable = self.controller.shared_data["receipt_no"])
order_number_entry.insert(0,self.controller.shared_data["random_receipt_no"])