Python Tkinter获取选中单选按钮文本值及内容同步实现
Tkinter单选按钮与Entry组件同步问题解决
我在Python Tkinter项目中使用单选按钮供用户选择,选中单选按钮时会向Entry组件填充对应文本。需要实现两个功能:
- 获取当前选中单选按钮的文本值
- 当用户修改Entry组件中的文本时,同步更新对应的文本消息变量
我曾尝试获取frameBtm的子元素列表,但无法判断哪个单选按钮处于选中状态。
初始代码
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()
尝试的方法
我尝试获取frameBtm的子元素列表,但无法判断哪个单选按钮处于选中状态:
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>]
解决方案
在@Brian Oakley的帮助下,问题得以解决:通过绑定Entry组件的回车键事件,使用btn_radio.get()方法结合条件判断确定选中的单选按钮,实现内容同步更新。
解决代码如下:
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)
内容的提问来源于stack exchange,提问作者xlmaster
相关产品推荐
相关产品推荐

