Search code examples
pythonuser-interfacetkinterradio-buttontkinter-button

How to create message box to show error message when no radio button is selected in Tkinter?


When I click a proceed button in window 2, I want to show error message when no radio button selected. but it shows error like " if v != [1,2,3]: NameError: name 'v' is not defined"

class Win2(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        Label(self, text= "second window", font= ('Helvetica 20 bold')).pack(padx=5, pady=10)
        Label(self, text= "Configurations", font= ('Helvetica 20 bold')).pack(padx=5, pady=10)

        v = StringVar(self, "1")

        values = {"block 1" : "1",
                  "block 2" : "2",
                  "block 3" : "3"}

        for (text, value) in values.items():
            Radiobutton(self, text = text, variable = v, bg="light blue", value = value, indicator = 0, width = 10).pack(pady=5)
        
     
        B1=Button(self, text="PROCEED", bg="green", fg="white", command=lambda:self.message()).pack(pady=10)
        B2=Button(self, text="BACK", bg="red", fg="white").pack(pady=10)

    def message(self):
        if v != [1,2,3]:
            messagebox.showerror(title="Error", message="Please select any configuration")
                

class Application(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        window = tk.Frame(self)
        window.pack(side = "top", fill = "both", expand = True)
        self.frames = {}
        for F in (Win1, Win2):
            frame = F(window, self)
            self.frames[F] = frame
            frame.grid(row = 0, column=0, sticky="nsew")
        self.show_frame(Win1)
        
    def show_frame(self, window):
        frame = self.frames[window]
        frame.tkraise()
        self.title("Test") 
        self.geometry('1500x1500')
        
app = Application()
app.mainloop()

Anyone please help me to solve this error


Solution

  • There seems to be a lot of confusions in your code. I suggest you read about python basic data structures (list, dict), types (str, int...) and class.

    I modified your code so that v is initialized with value "0". Only in this case will the messagebox show up by clicking "PROCEED". Once you start clicking one of the radiobuttons, it will always have value "1", "2" or "3":

    import tkinter as tk
    from tkinter import messagebox
    
    class Win2(tk.Frame):
        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            self.controller = controller
            tk.Label(self, text= "second window", font= ('Helvetica 20 bold')).pack(padx=5, pady=10)
            tk.Label(self, text= "Configurations", font= ('Helvetica 20 bold')).pack(padx=5, pady=10)
    
            self.v = tk.StringVar(self, "0")
    
            values = {"block 1" : "1",
                      "block 2" : "2",
                      "block 3" : "3"}
    
            for (text, value) in values.items():
                tk.Radiobutton(self, text = text, variable = self.v, bg="light blue", value = value, indicator = 0, width = 10).pack(pady=5)
                
            B1=tk.Button(self, text="PROCEED", bg="green", fg="white", command=lambda:self.message()).pack(pady=10)
            B2=tk.Button(self, text="BACK", bg="red", fg="white").pack(pady=10)
    
        def message(self):
            if self.v.get() not in ["1","2","3"]:
                messagebox.showerror(title="Error", message="Please select any configuration")
                    
    
    class Application(tk.Tk):
        def __init__(self, *args, **kwargs):
            tk.Tk.__init__(self, *args, **kwargs)
            window = tk.Frame(self)
            window.pack(side = "top", fill = "both", expand = True)
            frame = Win2(window, self)
            frame.grid(row = 0, column=0, sticky="nsew")
    
    
    app = Application()
    app.mainloop()