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

Python Tkinter GUI表单如何获取Radiobutton与Checkbox值并展示

问题修复方案

一、现有代码的核心问题

  • 单选按钮绑定错误:Radiobutton的variable参数错误传入字符串"var",没有绑定实际的IntVar变量var,且未绑定genSelect触发函数
  • genSelect逻辑错误:两个判断分支都返回Female,1对应男性的逻辑写反
  • submit函数取值错误:print()函数返回值为None,赋值给gen1会导致性别取值为空;复选框直接取控件对象的get(),实际应该取绑定的CheckVar系列变量值;列表框states取值方式错误
  • genSelect从未触发:单选按钮没有绑定command=genSelect,函数属性genSelect.gen从未被初始化

二、具体修改方法

1. 单选按钮部分修改

先修正genSelect逻辑,再修正单选按钮的绑定参数:

def genSelect():
    choice = var.get()
    if choice == 1:
        genSelect.gen = "Male"
    elif choice == 2:
        genSelect.gen = "Female"
    else:
        genSelect.gen = "未选择"

# 修正单选按钮代码,注意variable和value不要加引号,绑定command
R1 = Radiobutton(lb1, text="Male", variable=var, value=1, command=genSelect)
R1.place(x=120, y=110)
R2 = Radiobutton(lb1, text="Female", variable=var, value=2, command=genSelect)
R2.place(x=180, y=110)

submit中取性别值时直接调用genSelect()后读取属性即可:

genSelect() # 触发一次获取最新选择
gen1 = genSelect.gen

2. 复选框部分修改

不需要单独写同类函数,直接在submit中判断每个CheckVar的值,收集选中的兴趣文本即可:

hobby_list = []
if CheckVar1.get() == 1:
    hobby_list.append("Sports")
if CheckVar2.get() == 1:
    hobby_list.append("Music")
if CheckVar3.get() == 1:
    hobby_list.append("Cooking")
if CheckVar4.get() == 1:
    hobby_list.append("Reading Books")
if CheckVar5.get() == 1:
    hobby_list.append("Gaming")
hobbys = "、".join(hobby_list) if hobby_list else "未选择"

3. 列表框取值修正

states是单选列表框,需要先获取选中索引再取值:

selected_state = states.curselection()
st = states.get(selected_state[0]) if selected_state else "未选择"

三、修改后完整可运行代码

from tkinter import *
from tkinter import Button, Entry, Label, messagebox, ttk

top = Tk()
top.geometry("400x600")

def genSelect():
    choice = var.get()
    if choice == 1:
        genSelect.gen = "Male"
    elif choice == 2:
        genSelect.gen = "Female"
    else:
        genSelect.gen = "未选择"

def submit():
    namevar = e1.get()
    emailvar = e2.get()
    phonevar = e3.get()
    # 取性别
    genSelect()
    gen1 = genSelect.gen
    # 取兴趣
    hobby_list = []
    if CheckVar1.get() == 1:
        hobby_list.append("Sports")
    if CheckVar2.get() == 1:
        hobby_list.append("Music")
    if CheckVar3.get() == 1:
        hobby_list.append("Cooking")
    if CheckVar4.get() == 1:
        hobby_list.append("Reading Books")
    if CheckVar5.get() == 1:
        hobby_list.append("Gaming")
    hobbys = "、".join(hobby_list) if hobby_list else "未选择"
    # 取地区
    con=country.get()
    selected_state = states.curselection()
    st = states.get(selected_state[0]) if selected_state else "未选择"
    # 弹窗展示
    messagebox.showinfo("Your details", f"Your Name is: {namevar}\n Email: {emailvar}\n Phone: {phonevar}\n Gender: {gen1}\n Hobbys: {hobbys}\n Country: {con}\n State: {st}")

var = IntVar()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
CheckVar3 = IntVar()
CheckVar4 = IntVar()
CheckVar5 = IntVar()

register = Label(top, text="Register Form", font={})
register.place(x=120, y=5)

lb1 = LabelFrame(top, text="Personal Details")
lb1.place(height=160, width=390, x=5, y=35)

name = Label(lb1, text="Name")
name.place(x=50, y=20)
email = Label(lb1, text="Email")
email.place(x=50, y=50)
phone = Label(lb1, text="Phone no.")
phone.place(x=50, y=80)
gender = Label(lb1, text="Gender")
gender.place(x=50, y=110)

e1 = Entry(lb1)
e1.place(x=120, y=20)
e2 = Entry(lb1)
e2.place(x=120, y=50)
e3 = Entry(lb1)
e3.place(x=120, y=80)

R1 = Radiobutton(lb1, text="Male", variable=var, value=1, command=genSelect)
R1.place(x=120, y=110)
R2 = Radiobutton(lb1, text="Female", variable=var, value=2, command=genSelect)
R2.place(x=180, y=110)

lb2 = LabelFrame(top, text="Hobby")
lb2.place(height=70, width=390, x=5, y=200)

c1 = Checkbutton(lb2, text="Sports", variable=CheckVar1, onvalue=1, offvalue=0)
c1.place(x=5, y=12)
c2 = Checkbutton(lb2, text="Music", variable=CheckVar2, onvalue=1, offvalue=0)
c2.place(x=80, y=12)
c3 = Checkbutton(lb2, text="Cooking", variable=CheckVar3, onvalue=1, offvalue=0)
c3.place(x=155, y=12)
c4 = Checkbutton(lb2, text="Reading Books", variable=CheckVar4, onvalue=1, offvalue=0)
c4.place(x=230, y=12)
c5 = Checkbutton(lb2, text="Gaming", variable=CheckVar5, onvalue=1, offvalue=0)
c5.place(x=305, y=12)

lb3 = LabelFrame(top, text="Address")
lb3.place(height=205, width=390, x=5, y=277)

country_name = Label(lb3, text="Country")
country_name.place(x=10, y=8)
state_name = Label(lb3, text="State")
state_name.place(x=10, y=38)

country = ttk.Combobox(lb3, values=
[
    "INDIA",
    "Australia",
    "Russia",
    "Egypt",
    "U.S.A",
    "Canada",
    "Argentina"
])
country.current(0)
country.place(x=65, y=8)

states = Listbox(lb3, selectmode=SINGLE, height=8, width=20)
states.insert(1, "Maharashtra")
states.insert(2, "Madhya Pradesh")
states.insert(3, "Kerala")
states.insert(4, "Orissa")
states.insert(5, "Punjab")
states.insert(6, "Rajasthan")
states.insert(7, "Assam")
states.insert(8, "Gujarat")

states.place(x=65, y=42)

b1 = Button(top, command=submit, text="Submit", font={"arial", 12}, relief="solid")
b1.place(x=150, y=520)

top.mainloop()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 18:39:02