在tkinter中获取或打印当前单选按钮的值,可以通过以下步骤实现:
import tkinter as tk
from tkinter import messagebox
def get_selected_value():
selected_value = var.get()
messagebox.showinfo("Selected Value", f"The selected value is: {selected_value}")
var = tk.StringVar()
radio_button1 = tk.Radiobutton(window, text="Option 1", variable=var, value="Option 1")
radio_button2 = tk.Radiobutton(window, text="Option 2", variable=var, value="Option 2")
button = tk.Button(window, text="Get Selected Value", command=get_selected_value)
radio_button1.pack()
radio_button2.pack()
button.pack()
window.mainloop()
完整的代码示例:
import tkinter as tk
from tkinter import messagebox
def get_selected_value():
selected_value = var.get()
messagebox.showinfo("Selected Value", f"The selected value is: {selected_value}")
window = tk.Tk()
var = tk.StringVar()
radio_button1 = tk.Radiobutton(window, text="Option 1", variable=var, value="Option 1")
radio_button2 = tk.Radiobutton(window, text="Option 2", variable=var, value="Option 2")
button = tk.Button(window, text="Get Selected Value", command=get_selected_value)
radio_button1.pack()
radio_button2.pack()
button.pack()
window.mainloop()
这样,当用户选择一个单选按钮并点击"Get Selected Value"按钮时,将会弹出一个消息框显示当前选中的值。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云