0

I've looked at many questions and answers already:

None have the necessary info or are able to answer my question.

Here is a class in my app. This window is called when the user presses the "Mexican" button as a food choice on the previous page. On this page the user is presented with 3 options,

  1. YES - user likes the restaurant choice and the app quits
  2. TRY AGAIN - the user still wants Mexican food but not the choice given
  3. I Don't Want Mexican Anymore - self-explanatory, and goes back to choice of type of restaurant

My question refers to the TRY AGAIN option. I've tried Tk.update, creating a refresh function, lamba function calls, like command=lambda: controller.show_frame(MexicanRest) but nothing refreshes the current page and allows for the mex_choice = random.choice(mexican_rest) command to be called again and pick a new restaurant from the mexican_rest list defined.

You'll see where I've marked the `#WHAT GOES HERE TO REFRESH?" Please advise...

class MexicanRest(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Mexican Restaurant", 
                        font=('century gothic', 24))
        label.pack(pady=10, padx=10)

        mex_choice = random.choice(mexican_rest)

        label = tk.Label(self, 
                        text="How does %s sound? " %mex_choice, 
                        bg='yellow', 
                        font=('times', 24), 
                        fg='red')
        label.pack()

        button1 = tk.Button(self, 
                        text='YES', 
                        width=20, 
                        command=quit)
        button1.pack(fill=tk.BOTH)

        button2 = tk.Button(self, 
                        text='TRY AGAIN', 
                        width=20, 
                        command=self.refresh) #WHAT GOES HERE TO REFRESH???
        button2.pack(fill=tk.BOTH)

        button3 = tk.Button(self, 
                        text='I Don\'t Want Mexican Anymore', 
                        #width=20, 
                        command=lambda: controller.show_frame(StartPage))
        button3.pack(fill=tk.BOTH)

    def refresh(self):
        MexicanRest.update(self)
MacItaly
  • 345
  • 4
  • 16
  • 1
    tkinter can't automagically update windows. You have to write the code to do that. – Bryan Oakley Mar 01 '19 at 19:58
  • @BryanOakley Then what can I add into the refresh() method to update the window? +1 for "automagically" - I like that and will be using it from now on. – MacItaly Mar 01 '19 at 19:59
  • Are you aware that all widgets have multiple methods for setting and clearing the values displayed in the widgets? You just need to call those methods on the widgets you want to be updated. – Bryan Oakley Mar 01 '19 at 20:00
  • @BryanOakley That doesn't help at all. Example please... – MacItaly Mar 01 '19 at 20:12

1 Answers1

1

You need to do two things:

  1. Make it so that it's possible to access the widgets that should be refreshed (eg: make them instance attributes)
  2. Write a function to reconfigure those widgets

In your case, the first step is to create an attribute for the label you want to refresh. For example:

self.choice_label = tk.Label(self, 
                text="How does %s sound? " %mex_choice, 
                bg='yellow', 
                font=('times', 24), 
                fg='red')
self.choice_label.pack()

Next, create a function that updates that label:

def refresh(self):
    mex_choice = random.choice(mexican_rest)
    self.choice_label.configure(text="How does %s sound? " %mex_choice)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685