python tkinter getting text value from selected radiobutton
Question:
I have a radiobuttons, for user to select. On each selection it gives to Entry widget some text.
Now, I want to get the text value from the radiobutton which is selected
. As I remember checkboxes have true or false values, but I used radio buttons on my project.
screen
I need to know, which radio button is selected, and if user changes text on entry widget, then I want to change the text message variables accordingly.
and here is my code
from tkinter import ttk
from tkinter import *
import yagmail
root=Tk()
root.title('send bunch of inquiry emails')
#frame entry for search and button
frameUpper=Frame(master=root,borderwidth=3,relief='flat',padx=12,pady=12)
frameUpper.grid(row=0,column=0,sticky=(E,W))
#frame for text to send via email
frameBtm=Frame(master=root,borderwidth=3,relief='groove',padx=12,pady=12,width=200,height=100)
frameBtm.grid(row=1,column=0,sticky=(N,S,E,W))
frm_emails=ttk.Frame(master=root,padding=3,borderwidth=3,width=300,relief='sunken')
var_inp=StringVar()
lbl_search=Label(master=frameUpper,text='enter search keyword:')
entry_search=Entry(master=frameUpper,width=33,textvariable=var_inp,relief='groove')
#text editor
txt_greetings='Hi,'
search_variable='keyword'
txt_body=f'I would like to have price information for {search_variable} for our company located in Azerbaijan'
txt_regards='Kind regards,'
btn_radio=StringVar()
btn_radio_greeting=ttk.Radiobutton(master=frameBtm,text='Greetings',variable=btn_radio,value=txt_greetings)
btn_radio_body=ttk.Radiobutton(master=frameBtm,text='Body',variable=btn_radio,value=txt_body)
btn_radio_regards=ttk.Radiobutton(master=frameBtm,text='Regards',variable=btn_radio,value=txt_regards)
lbl_text_message=Label(master=frameBtm,text='text message for email')
entry_msg_txt=StringVar()
entry_text=Entry(master=frameBtm,textvariable=entry_msg_txt,borderwidth=1,width=20)
def func_x():
frm_emails.grid(row=2,sticky=(S,E,W))
Label(master=frm_emails, text=no_duplicates,wraplength=200).pack()
lbl_search.grid(sticky=(E,W))
entry_search.grid(sticky=(E,W))
entry_search.bind('<Return>', start_scraping)
Button(master=frameUpper, width=13, command=start_scraping, text='run').grid(pady=5)
btn_radio_greeting.grid(row=1,column=0,sticky=(W,))
btn_radio_body.grid(row=2,column=0,sticky=(W,))
btn_radio_regards.grid(row=3,column=0,sticky=(W,))
btn_radio_greeting.bind('<Double-1>',change_value_btn_greetings)
btn_radio_body.bind('<Double-1>',change_value_btn_body)
btn_radio_regards.bind('<Double-1>',change_value_btn_regards)
lbl_text_message.grid(row=0,column=0,rowspan=1,columnspan=1,sticky=(W))
entry_text.grid(row=1,column=1,rowspan=3,columnspan=3,sticky=(N,S,E,W))
root.columnconfigure(0,weight=1)
root.rowconfigure(1,weight=1)
frameUpper.columnconfigure(0,weight=1)
frameBtm.columnconfigure(0,weight=1)
frameBtm.rowconfigure(0,weight=1)
frameBtm.columnconfigure(1,weight=5)
frameBtm.rowconfigure(1,weight=5)
lbl_text_message.rowconfigure(0,weight=1)
lbl_text_message.columnconfigure(0,weight=1)
entry_text.rowconfigure(1,weight=5)
entry_text.columnconfigure(1,weight=5)
frm_emails.rowconfigure(0,weight=1)
frm_emails.columnconfigure(0,weight=1)
##newly added lines
def get_value_from_entry_txt_put_to_variable(event):
global txt_greetings
global txt_body
global txt_regards
global txt_message_to_send
if btn_radio_regards.winfo_viewable()==1:
txt_regards = entry_msg_txt.get()
elif btn_radio_body.winfo_viewable()==1:
txt_body = entry_msg_txt.get()
elif btn_radio_greeting.winfo_viewable()==1:
txt_greetings = entry_msg_txt.get()
print(txt_message_to_send)
txt_message_to_send = txt_greetings + 'n' + txt_body + 'n' + txt_regards
test_print()
entry_text.bind('<Return>',get_value_from_entry_txt_put_to_variable)
# send emails
txt_message_to_send=txt_greetings+'n'+txt_body+'n'+txt_regards
def test_print():
print(txt_message_to_send)
if __name__=='__main__':
root.mainloop()
I am able to get children elements in frameBottom as a list, but do not know, how to get to know that which radio button is selected actually?
a=[x for x in frameBtm.winfo_children()]
print(a)
[<tkinter.ttk.Radiobutton object .!frame2.!radiobutton>, <tkinter.ttk.Radiobutton object .!frame2.!radiobutton2>, <tkinter.ttk.Radiobutton object .!frame2.!radiobutton3>, <tkinter.Label object .!frame2.!label>, <tkinter.Entry object .!frame2.!entry>]
With respond assistance from @Brian Oakley i resolved my issue
I bounded enter key, for entry widget modifictions, then with help of get()
method i used conditionals to determine which radiobutton value
is selected. The code is below.
def get_value_from_entry_txt_put_to_variable(event):
global txt_greetings
global txt_body
global txt_regards
global txt_message_to_send
print(btn_radio.get())
if btn_radio.get()==txt_regards:
print('btn_radio_regards')
txt_regards = entry_msg_txt.get()
elif btn_radio.get()==txt_body:
print('btn_radio_body')
txt_body = entry_msg_txt.get()
elif btn_radio.get()==txt_greetings:
print('btn_radio_greeting')
txt_greetings = entry_msg_txt.get()
print(txt_message_to_send,'---')
txt_message_to_send = txt_greetings + 'n' + txt_body + 'n' + txt_regards
print(txt_message_to_send, '-+-')
test_print()
entry_text.bind('<Return>',get_value_from_entry_txt_put_to_variable)
Answers:
You need to give each radio button a value
. You can then call get
on the associated variable to get the selected value.
I have a radiobuttons, for user to select. On each selection it gives to Entry widget some text.
Now, I want to get the text value from the radiobutton which is selected
. As I remember checkboxes have true or false values, but I used radio buttons on my project.
screen
I need to know, which radio button is selected, and if user changes text on entry widget, then I want to change the text message variables accordingly.
and here is my code
from tkinter import ttk
from tkinter import *
import yagmail
root=Tk()
root.title('send bunch of inquiry emails')
#frame entry for search and button
frameUpper=Frame(master=root,borderwidth=3,relief='flat',padx=12,pady=12)
frameUpper.grid(row=0,column=0,sticky=(E,W))
#frame for text to send via email
frameBtm=Frame(master=root,borderwidth=3,relief='groove',padx=12,pady=12,width=200,height=100)
frameBtm.grid(row=1,column=0,sticky=(N,S,E,W))
frm_emails=ttk.Frame(master=root,padding=3,borderwidth=3,width=300,relief='sunken')
var_inp=StringVar()
lbl_search=Label(master=frameUpper,text='enter search keyword:')
entry_search=Entry(master=frameUpper,width=33,textvariable=var_inp,relief='groove')
#text editor
txt_greetings='Hi,'
search_variable='keyword'
txt_body=f'I would like to have price information for {search_variable} for our company located in Azerbaijan'
txt_regards='Kind regards,'
btn_radio=StringVar()
btn_radio_greeting=ttk.Radiobutton(master=frameBtm,text='Greetings',variable=btn_radio,value=txt_greetings)
btn_radio_body=ttk.Radiobutton(master=frameBtm,text='Body',variable=btn_radio,value=txt_body)
btn_radio_regards=ttk.Radiobutton(master=frameBtm,text='Regards',variable=btn_radio,value=txt_regards)
lbl_text_message=Label(master=frameBtm,text='text message for email')
entry_msg_txt=StringVar()
entry_text=Entry(master=frameBtm,textvariable=entry_msg_txt,borderwidth=1,width=20)
def func_x():
frm_emails.grid(row=2,sticky=(S,E,W))
Label(master=frm_emails, text=no_duplicates,wraplength=200).pack()
lbl_search.grid(sticky=(E,W))
entry_search.grid(sticky=(E,W))
entry_search.bind('<Return>', start_scraping)
Button(master=frameUpper, width=13, command=start_scraping, text='run').grid(pady=5)
btn_radio_greeting.grid(row=1,column=0,sticky=(W,))
btn_radio_body.grid(row=2,column=0,sticky=(W,))
btn_radio_regards.grid(row=3,column=0,sticky=(W,))
btn_radio_greeting.bind('<Double-1>',change_value_btn_greetings)
btn_radio_body.bind('<Double-1>',change_value_btn_body)
btn_radio_regards.bind('<Double-1>',change_value_btn_regards)
lbl_text_message.grid(row=0,column=0,rowspan=1,columnspan=1,sticky=(W))
entry_text.grid(row=1,column=1,rowspan=3,columnspan=3,sticky=(N,S,E,W))
root.columnconfigure(0,weight=1)
root.rowconfigure(1,weight=1)
frameUpper.columnconfigure(0,weight=1)
frameBtm.columnconfigure(0,weight=1)
frameBtm.rowconfigure(0,weight=1)
frameBtm.columnconfigure(1,weight=5)
frameBtm.rowconfigure(1,weight=5)
lbl_text_message.rowconfigure(0,weight=1)
lbl_text_message.columnconfigure(0,weight=1)
entry_text.rowconfigure(1,weight=5)
entry_text.columnconfigure(1,weight=5)
frm_emails.rowconfigure(0,weight=1)
frm_emails.columnconfigure(0,weight=1)
##newly added lines
def get_value_from_entry_txt_put_to_variable(event):
global txt_greetings
global txt_body
global txt_regards
global txt_message_to_send
if btn_radio_regards.winfo_viewable()==1:
txt_regards = entry_msg_txt.get()
elif btn_radio_body.winfo_viewable()==1:
txt_body = entry_msg_txt.get()
elif btn_radio_greeting.winfo_viewable()==1:
txt_greetings = entry_msg_txt.get()
print(txt_message_to_send)
txt_message_to_send = txt_greetings + 'n' + txt_body + 'n' + txt_regards
test_print()
entry_text.bind('<Return>',get_value_from_entry_txt_put_to_variable)
# send emails
txt_message_to_send=txt_greetings+'n'+txt_body+'n'+txt_regards
def test_print():
print(txt_message_to_send)
if __name__=='__main__':
root.mainloop()
I am able to get children elements in frameBottom as a list, but do not know, how to get to know that which radio button is selected actually?
a=[x for x in frameBtm.winfo_children()]
print(a)
[<tkinter.ttk.Radiobutton object .!frame2.!radiobutton>, <tkinter.ttk.Radiobutton object .!frame2.!radiobutton2>, <tkinter.ttk.Radiobutton object .!frame2.!radiobutton3>, <tkinter.Label object .!frame2.!label>, <tkinter.Entry object .!frame2.!entry>]
With respond assistance from @Brian Oakley i resolved my issue
I bounded enter key, for entry widget modifictions, then with help of get()
method i used conditionals to determine which radiobutton value
is selected. The code is below.
def get_value_from_entry_txt_put_to_variable(event):
global txt_greetings
global txt_body
global txt_regards
global txt_message_to_send
print(btn_radio.get())
if btn_radio.get()==txt_regards:
print('btn_radio_regards')
txt_regards = entry_msg_txt.get()
elif btn_radio.get()==txt_body:
print('btn_radio_body')
txt_body = entry_msg_txt.get()
elif btn_radio.get()==txt_greetings:
print('btn_radio_greeting')
txt_greetings = entry_msg_txt.get()
print(txt_message_to_send,'---')
txt_message_to_send = txt_greetings + 'n' + txt_body + 'n' + txt_regards
print(txt_message_to_send, '-+-')
test_print()
entry_text.bind('<Return>',get_value_from_entry_txt_put_to_variable)
You need to give each radio button a value
. You can then call get
on the associated variable to get the selected value.