Python Forum
problem with radio button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with radio button
#1
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()
Reply
#2
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()
Reply
#3
thank you, its working very correctly.
But what to do to make these 4 checkboxes unchecked when starting the program?
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Is there a way to determine if a radio button has been selected? TWB 5 7,691 Jan-31-2023, 09:44 AM
Last Post: Vadanane
  [Tkinter] Radio Buttons Working Bassackwards gw1500se 6 2,936 Dec-07-2021, 07:13 PM
Last Post: menator01
  [Tkinter] Grid the radio buttons Joni_Engr 6 6,091 Nov-24-2021, 07:20 PM
Last Post: menator01
  Radio butto to enable/disable combo box in Tkinter cybertooth 5 6,710 Oct-09-2021, 07:30 AM
Last Post: cybertooth
  tkinter python button position problem Nick_tkinter 3 4,484 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  [Tkinter] Button click problem using OOP JohnB 5 4,611 Oct-21-2020, 12:43 PM
Last Post: JohnB
  [Tkinter] I can't get information from a radio button aquerci 2 3,134 May-20-2020, 10:31 AM
Last Post: aquerci
  Problem about image and button scotesse 5 3,615 Apr-27-2020, 10:09 AM
Last Post: scotesse
  Problem with Submit button Tkinter Reldaing 2 4,127 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa
  [PyQt] Problem how to click a button inside a group box? mart79 2 4,006 Aug-05-2019, 01:21 PM
Last Post: mart79

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020