Home > front end >  How do I set a random number within a shared data variable in different class
How do I set a random number within a shared data variable in different class

Time:03-31

I am trying to get a random number generated within the text entry box. It is not working since I'm not sure how to share the variable. I am using python tkinter in visual studio code if that helps. The variable receipt_no is supposed to generate a random number between 1000-9999 in a text entry box. I get an error which is '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()

I have tried to set the value and try share it to a different class. I am expecting to get a random generated number between 1000-9999.

CodePudding user response:

Since you are creating a dictionary to get the receipt_no value to set the text of Entry widget with, don't generate its value using .set, just create one tk.IntVar() variable and one string variable to store the random number.

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

Now, to set the text of Entry widgets, you might want to use insert method:

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"])
  • Related