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
  Radio butto to enable/disable combo box in Tkinter cybertooth 5 219 Oct-09-2021, 07:30 AM
Last Post: cybertooth
  tkinter python button position problem Nick_tkinter 3 1,098 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  [Tkinter] Button click problem using OOP JohnB 5 1,014 Oct-21-2020, 12:43 PM
Last Post: JohnB
  [Tkinter] I can't get information from a radio button aquerci 2 1,343 May-20-2020, 10:31 AM
Last Post: aquerci
  Problem about image and button scotesse 5 1,155 Apr-27-2020, 10:09 AM
Last Post: scotesse
  Problem with Submit button Tkinter Reldaing 2 1,555 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa
  [PyQt] Problem how to click a button inside a group box? mart79 2 1,445 Aug-05-2019, 01:21 PM
Last Post: mart79
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 2,559 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Radio button help Muzz 5 1,829 Apr-28-2019, 07:43 AM
Last Post: Muzz
  [Tkinter] problem with button events Lubik_ 4 7,166 Dec-01-2017, 08:47 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

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