Python Forum

Full Version: problem with radio button
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello i have a problem with radiobutton. I would like to have the following effect:
1. All radio buttons should be unchecked at the start.
2. When I mark 'OK' and press 'PRESS' in the text window 'OK' should appear - and it works
3. Same with 'OV' and 'RE' - it works.
4. When I click on 'Other' the 'Entry' field becomes active and when I enter for example 'XX' there and press 'Press' in the 'Text' field 'XX' should appear.
Unfortunately option 4 does not work, I tried to set it by variable somehow but I fail.
I would be grateful for your suggestions.

import tkinter as tk
from tkinter import *

def initWindow():
    root = tk.Tk()
    root.geometry('500x260')
    return root

root = initWindow()

var = tk.StringVar() 

def change1():
    field_Rest.config(state='normal')

def change2():
    field_Rest.delete(0,END)
    field_Rest.config(state='disabled')

def click_1():
    field_result.delete(1.0, END)
    field_result.insert(1.0, var.get())
    
   
bplick = tk.Button(root,text='Press', width=5, command=click_1)
bplick.place(x=340,y=10)
field_Rest = Entry(root, width=4,state='disabled')
field_Rest.place(x=230,y=10)

chbRE = tk.Radiobutton(root,text='RE',variable=var, value='RE', command=change2)
chbRE.place(x=20,y=10)
chbOK = tk.Radiobutton(root,text='OK',variable=var, value='OK', command=change2)
chbOK.place(x=70,y=10)
chbOV = tk.Radiobutton(root,text='OV',variable=var, value='OV', command=change2)
chbOV.place(x=120,y=10)

x2 = str(field_Rest.get())
print(x2)

chbOther = tk.Radiobutton(root,text='Other',variable=var, value=str(x2), command=change1)
chbOther.place(x=170,y=10)

field_result = Text(root, width=25, height=10) 
field_result.place(x=50,y=45)

root.mainloop()
You need to grab the current value of field_rest at the point of clicking the button if the other checkbox is selected.

import tkinter as tk


def init_window():
    root = tk.Tk()
    root.geometry("500x260")
    return root


root = init_window()

var = tk.StringVar()


def change1():
    field_rest.config(state="normal")


def change2():
    field_rest.delete(0, tk.END)
    field_rest.config(state="disabled")


def click_1():
    result = field_rest.get() if var.get() == "Other" else var.get()
    field_result.delete(1.0, tk.END)
    field_result.insert(1.0, result)


bplick = tk.Button(root, text="Press", width=5, command=click_1)
bplick.place(x=340, y=10)
field_rest = tk.Entry(root, width=4, state="disabled")
field_rest.place(x=230, y=10)

chb_re = tk.Radiobutton(root, text="RE", variable=var, value="RE", command=change2)
chb_re.place(x=20, y=10)
chb_ok = tk.Radiobutton(root, text="OK", variable=var, value="OK", command=change2)
chb_ok.place(x=70, y=10)
chb_ov = tk.Radiobutton(root, text="OV", variable=var, value="OV", command=change2)
chb_ov.place(x=120, y=10)


chb_other = tk.Radiobutton(
    root, text="Other", variable=var, value="Other", command=change1
)
chb_other.place(x=170, y=10)

field_result = tk.Text(root, width=25, height=10)
field_result.place(x=50, y=45)


root.mainloop()
thank you, its working very correctly.
But what to do to make these 4 checkboxes unchecked when starting the program?
var.set(None)
But it is more common to select a default instead of leaving a radio cluster empty. Replace None above with the default value.