简体   繁体   中英

Python tkinter dynamic entry box with Radiobutton

I want to have dynamically enabled entry box associated with the Radiobutton in the tkinter UI. Below is my code,

from tkinter import *
from tkinter import filedialog
from tkinter.ttk import * 
import tkinter.messagebox
root = Tk()

tkinter.Label(root, text="Choose:").grid(row=1, column=1, sticky=W)
var3 = IntVar(value=1)
Radiobutton(root, text="Option 1", variable=var3, value=1).grid(row=1, column=2, sticky=W)

entry = tkinter.Entry(root,  width="10")
entry.insert(10, 'Text') 
entry.grid(row=5, column=4, sticky=W)
entry.configure(state='disabled')

def naccheck(entry, var3):
    if var3.get() != 2:
        entry.configure(state='disabled')
    else:
        entry.configure(state='normal')

Radiobutton(root, text="Option 2", variable=var3, value=2, command=lambda e=entry, v=var3: naccheck(e,v)).grid(row=5, column=2, sticky=E)
root.mainloop()

Only problem is the entry box is not getting dynamically updated based on whether "Option 2" is selected or not.

In this problem, You have to just add the function command to Option 1 also.So, whenever the Radiobutton is checked, the function will called and condition checked every time.

Also you have to create widgets that are calls function in tkinter, you have to declare them after function declaration.

Here's a solution

from tkinter import *
from tkinter import filedialog
from tkinter.ttk import *
import tkinter.messagebox

root = Tk()

tkinter.Label(root, text="Choose:").grid(row=1, column=1, sticky=W)
var3 = IntVar(value=1)

entry = tkinter.Entry(root,  width="10")
entry.insert(10, 'Text')
entry.grid(row=5, column=4, sticky=W)
entry.configure(state='disabled')

def naccheck(entry, var3):
    if var3.get() != 2:
        entry.configure(state='disabled')
    else:
        entry.configure(state='normal')

rbtn1 = Radiobutton(root, text="Option 1", variable=var3, value=1,command=lambda e=entry, v=var3: naccheck(e,v))
rbtn1.grid(row=1, column=2, sticky=W)

rbtn2 = Radiobutton(root, text="Option 2", variable=var3, value=2, command=lambda e=entry, v=var3: naccheck(e,v))
rbtn2.grid(row=5, column=2, sticky=E)

root.mainloop()

A simple change that solved this was you need to give the same function callback to both the Radiobutton widgets. And you don't need to pass any arguments to the callback function.

from tkinter import *
from tkinter import filedialog
from tkinter.ttk import * 
import tkinter.messagebox

root = Tk()

tkinter.Label(root, text="Choose:").grid(row=1, column=1, sticky=W)

entry = tkinter.Entry(root, width="10")
entry.insert(10, 'Text') 
entry.grid(row=5, column=4, sticky=W)
entry.configure(state='disabled')

var3 = IntVar(value=1)

def naccheck():
    if var3.get() == 2:
        entry.configure(state='normal')
    else:
        entry.configure(state='disable')

# You need to pass the same command function to both the Radiobutton widgets
Radiobutton(root, text="Option 1", variable=var3, value=1, command=naccheck).grid(row=1, column=2, sticky=W)
Radiobutton(root, text="Option 2", variable=var3, value=2, command=naccheck).grid(row=5, column=2, sticky=E)

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM