You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python tkinter:实现点击保存按钮修改L2标签文本

修改Tkinter娱乐评论界面的保存提示功能

要实现点击「保存评论」按钮后,将l2标签的文本从英文提示改为中文的「您的评论已保存」,只需添加触发函数并绑定到保存按钮即可,具体修改如下:

修改后的完整代码

#Frame for displaying message to user about critque status
frame_two=tk.Frame(entertainment_critique_window, width=622, height=140, highlightbackground= 'black', highlightthickness=6)
frame_two.grid(row=0, column=2,sticky='N', padx=5, pady=5, ipadx=10,ipady=10, columnspan=2)
frame_two.grid_propagate(False)

radiobutton_variable = StringVar(entertainment_critique_window, 1)


def update_status():
    l2.config(text='您已选择 ' + str(radiobutton_variable.get()))

# 新增保存触发函数
def save_critique():
    l2.config(text='您的评论已保存')

l1=Label(frame_two, text="评论状态:", fg='white', font=('Comic Sans',45, 'bold'))
l1.grid(row=0,column=2,sticky='NW',padx=0,pady=0)
l2=Label(frame_two, text="请从下方选项中选择", fg='white', font=('Comic Sans',27,))
l2.grid(row=1,column=2,sticky='E',padx=50,pady=20)

#Frame for displaying 3 Media (entertainment) options
frame_three=tk.Frame(entertainment_critique_window, width=642, height=200,
highlightbackground='black', highlightthickness=6)
frame_three.grid(row=0,column=2,sticky='NE', padx=5,pady=175)
frame_three.grid_propagate(False)


r1=tk.Radiobutton(frame_three, text="影院 - Hoyts", variable = radiobutton_variable, value="Cinema - Hoyts", command=update_status)
r1.grid(row = 4, column = 0,pady=15)

r2=tk.Radiobutton(frame_three, text="电视 - 第七频道", variable = radiobutton_variable, value="TV  - Channel 7", command=update_status)
r2.grid(row = 5, column = 0,pady=15)

r3=tk.Radiobutton(frame_three, text="电台 - Nova 100", variable = radiobutton_variable, value="Radio - Nova 100", command=update_status)
r3.grid(row = 6, column = 0,pady=15)



button=Button(frame_three,text="查看详情",height=1)
button.place(x=462,y=50,)

button=Button(frame_three, text="查看摘要",height=1)
button.place(x=455,y=120,)




#Frame for User to provide rating of selected option
frame_four=tk.Frame(entertainment_critique_window, width=642, height=130,
highlightbackground='black', highlightthickness=6)
frame_four.grid(row=0,column=2,sticky='NE', padx=5,pady=415)
frame_four.grid_propagate(False)

l3=Label(frame_four, text="评分:", fg='white', font=('Comic Sans',35, 'bold'))
l3.grid(row=0,column=2,sticky='NW',padx=0,pady=0)

# 给保存按钮绑定触发函数
button_save=Button(frame_four, text="保存评论",width=10,height=1, command=save_critique)
button_save.place(x=495,y=12,)

spinbox1= Spinbox(frame_four, from_=0, to=10, width=15)
spinbox1.place(x=300,y=14,)

# Start the event loop to detect user inputs
entertainment_critique_window.mainloop()

关键修改点

  • 新增save_critique()函数,负责更新l2标签的文本为「您的评论已保存」
  • 将button_save的command属性设置为save_critique,实现点击触发
  • 顺带将界面中的英文提示文本统一翻译成中文,提升用户体验

内容的提问来源于stack exchange,提问作者TP JJB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 08:58:11