Hello,
I have this code which is working

import Tkinter as tk
from Tkinter import *


LARGE_FONT= ("Verdana", 12)

class ChangePages(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack()
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        for F in (MainPage, Page01, Page02):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(MainPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

#MainPage
class MainPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)

        button1=Button(self,text='Go To Page 1',fg='blue',font=('Helvetica',26),height=5, width=20,command=lambda: controller.show_frame(Page01)).grid(row=1,column=1)

#Page01
class Page01(tk.Frame):

    def __init__(self, parent, controller):

        option1 = IntVar()
        option2 = IntVar()
        option3 = IntVar()

        tk.Frame.__init__(self, parent)
        f = Frame(self)
        f.pack(side='left')

        label1=Label(f,text='Select options',fg='blue', font=("Arial", 36, "bold"),width=54, relief='solid').grid(row=1,column=1,columnspan=3)

        labelspacing=Label(f,text='Option 1 ',font=("Arial", 18, "bold"),width=20,height=1).grid(row=2,column=1)
        labelspacing=Label(f,text='Option 2 ',font=("Arial", 18, "bold"),width=20,height=1).grid(row=2,column=2)
        labelspacing=Label(f,text='Option 3',font=("Arial", 18, "bold"),width=20,height=1).grid(row=2,column=3)

        buttonoption11=Radiobutton(f, text="Option 1 - A", variable=option1, value=1, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20).grid(row=3,column=1)
        buttonoption21=Radiobutton(f, text="Option 2 - A", variable=option2, value=1, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20).grid(row=3,column=2)
        buttonoption31=Radiobutton(f, text="Option 3 - A", variable=option3, value=1, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20).grid(row=3,column=3)

        buttonoption12=Radiobutton(f, text="Option 1 - B", variable=option1, value=2, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20).grid(row=4,column=1)
        buttonoption22=Radiobutton(f, text="Option 2 - B", variable=option2, value=2, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20).grid(row=4,column=2)
        buttonoption32=Radiobutton(f, text="Option 3 - B", variable=option3, value=2, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20).grid(row=4,column=3)

        buttonoption23=Radiobutton(f, text="Option 2 - C", variable=option2, value=3, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20).grid(row=5,column=2)

        buttonnback=Button(f,text='Back',fg='blue',font=('Helvetica',26),height=1,width=15,command=lambda: controller.show_frame(MainPage)).grid(row=10,column=1)
        buttonnext=Button(f,text='Next',fg='blue',font=('Helvetica',26),height=3,width=15,command=lambda: controller.show_frame(Page02)).grid(row=10,column=2)

#Page02
class Page02(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page 02!!!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(MainPage))
        button1.pack()

        button2 = tk.Button(self, text="Page 01",
                            command=lambda: controller.show_frame(PageSub35t))
        button2.pack()


#Root loop
app = ChangePages()
app.mainloop()

What i want to do and this is reason for asking help is :

When press next in pag01 ,i want to check states of radio buttons. For example if user choose "Option 1 -A" and "Option 2 -C" I want to popup a message saying "You are not allowed to chose 2c! you can choose 2c only if 1B is selected" and not let him to go to page02.

Thank you in advance

Recommended Answers

There is a better way: you can disable 2C when 1A is on. In the following code, I added a callback method, which is called when one of the options is selected. It is a very good way to prohibit some choices. It is completely configurable.
This code needs to be …

Jump to Post

All 2 Replies

Re: Python Tkinter check radio button state before proceed 80 80

There is a better way: you can disable 2C when 1A is on. In the following code, I added a callback method, which is called when one of the options is selected. It is a very good way to prohibit some choices. It is completely configurable.
This code needs to be completed, I only started the refactoring, but it already works.
Compare it to your code with a diff visualizer such as meld or kdiff3 or winmerge, etc.

import Tkinter as tk
from Tkinter import *
from functools import partial


LARGE_FONT= ("Verdana", 12)

class ChangePages(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack()
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        for F in (MainPage, Page01, Page02):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(MainPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

#MainPage
class MainPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)

        button1=Button(self,text='Go To Page 1',fg='blue',font=('Helvetica',26),height=5, width=20,command=lambda: controller.show_frame(Page01)).grid(row=1,column=1)

#Page01
class Page01(tk.Frame):

    def __init__(self, parent, controller):

        self.option1 = option1 = IntVar()
        self.option2 = option2 = IntVar()
        self.option3 = option3 = IntVar()

        tk.Frame.__init__(self, parent)
        f = Frame(self)
        f.pack(side='left')

        label1=Label(f,text='Select options',fg='blue', font=("Arial", 36, "bold"),width=54, relief='solid').grid(row=1,column=1,columnspan=3)

        labelspacing=Label(f,text='Option 1 ',font=("Arial", 18, "bold"),width=20,height=1).grid(row=2,column=1)
        labelspacing=Label(f,text='Option 2 ',font=("Arial", 18, "bold"),width=20,height=1).grid(row=2,column=2)
        labelspacing=Label(f,text='Option 3',font=("Arial", 18, "bold"),width=20,height=1).grid(row=2,column=3)

        def C(*args): return partial(self.option_changed, *args)

        buttonoption11=Radiobutton(f, text="Option 1 - A", variable=option1, value=1, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20, command=C(1,'A')).grid(row=3,column=1)
        buttonoption21=Radiobutton(f, text="Option 2 - A", variable=option2, value=1, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20, command=C(2,'A')).grid(row=3,column=2)
        buttonoption31=Radiobutton(f, text="Option 3 - A", variable=option3, value=1, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20, command=C(3,'A')).grid(row=3,column=3)

        buttonoption12=Radiobutton(f, text="Option 1 - B", variable=option1, value=2, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20, command=C(1,'B')).grid(row=4,column=1)
        buttonoption22=Radiobutton(f, text="Option 2 - B", variable=option2, value=2, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20, command=C(2,'B')).grid(row=4,column=2)
        buttonoption32=Radiobutton(f, text="Option 3 - B", variable=option3, value=2, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20, command=C(3,'B')).grid(row=4,column=3)

        self.buttonoption23=Radiobutton(f, text="Option 2 - C", variable=option2, value=3, indicatoron=0, fg='blue',font=('Helvetica',26),height=1, width=20, command=C(2,'C'))
        self.buttonoption23.grid(row=5,column=2)

        buttonnback=Button(f,text='Back',fg='blue',font=('Helvetica',26),height=1,width=15,command=lambda: controller.show_frame(MainPage)).grid(row=10,column=1)
        buttonnext=Button(f,text='Next',fg='blue',font=('Helvetica',26),height=3,width=15,command=lambda: controller.show_frame(Page02)).grid(row=10,column=2)

    def option_changed(self, number, letter, *args, **kwd):
        print(number, letter)
        if self.option1.get() == 1:
            self.buttonoption23.config(state=tk.DISABLED)
            if self.option2.get() == 3:
                self.option2.set(0)
        else:
            self.buttonoption23.config(state=tk.NORMAL)

#Page02
class Page02(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page 02!!!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(MainPage))
        button1.pack()

        button2 = tk.Button(self, text="Page 01",
                            command=lambda: controller.show_frame(PageSub35t))
        button2.pack()


#Root loop
app = ChangePages()
app.mainloop()
Re: Python Tkinter check radio button state before proceed 80 80

Thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.