0

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

  • v is a `StringVar` so you'll get its value with `get()`. `v != [1,2,3]` checks if v is not equal to a *list* containing the int 1, 2 and 3. I think you meant `if v.get() not in ["1","2","3"]`. But since you initialize `v` with the value `"1"`, this condition will likely never be True. – Tranbi Feb 13 '23 at 10:16
  • Also v is defined inside `__init__` so its scope is limited to this function. Make it an attribute of your class (`self.v`) or indent your `message` function to be defined inside `__init__` too – Tranbi Feb 13 '23 at 10:20
  • Can you tell me how to solve this. Because I am new to to programming. –  Feb 13 '23 at 11:10

1 Answers1

0

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()
Tranbi
  • 11,407
  • 6
  • 16
  • 33