管脚 2023-02-25 19:49 采纳率: 50%
浏览 17
已结题

py tkinter单选框相关问题

问题:以下代码中,输出的总是PY_VAR0 PY_VAR1,请问是怎么回事?我希望输出单选框中选中的结果需要怎么修改?


```python

import tkinter as tk


def imp():
    print(levelchoose,modechoose)

top = tk.Tk()
top.config(bg='#DEDEDE')
top.geometry('960x540')
#主要界面、字符
songs = tk.Frame(top,width=278,height=478,bd=1,bg='#B3B3B3',takefocus=False)
songs.place(x=32,y=33)
word1 = tk.Label(text='选择曲目',bg='#DEDEDE')
word1.place(x=139,y=10)

level = tk.Frame(top,width=278,height=220,bd=1,bg='#B3B3B3',takefocus=False)
level.place(x=352,y=33)
word2 = tk.Label(text='难度',bg='#DEDEDE')
word2.place(x=467,y=10)

mode = tk.Frame(top,width=278,height=230,bd=1,bg='#B3B3B3',takefocus=False)
mode.place(x=352,y=281)
word3 = tk.Label(text='模式',bg='#DEDEDE')
word3.place(x=467,y=255)

info = tk.Frame(top,width=278,height=180,bd=1,bg='#B3B3B3',takefocus=False)
info.place(x=658,y=33)
word4 = tk.Label(text='信息:还没做',bg='#b3b3b3')
word4.place(x=684,y=50)


#难度单选框
levelchoose = tk.StringVar()
levelchoose.set('1')
levelradio = tk.Radiobutton(level,variable=levelchoose,value='1',text='1',bg='#B3B3B3').place(x=10,y=10)
levelradio = tk.Radiobutton(level,variable=levelchoose,value='2',text='2',bg='#B3B3B3').place(x=10,y=30)
levelradio = tk.Radiobutton(level,variable=levelchoose,value='3',text='3',bg='#B3B3B3').place(x=10,y=50)
levelradio = tk.Radiobutton(level,variable=levelchoose,value='4',text='4',bg='#B3B3B3').place(x=10,y=70)
levelradio = tk.Radiobutton(level,variable=levelchoose,value='5',text='5',bg='#B3B3B3').place(x=10,y=90)

#模式单选框
modechoose = tk.StringVar()
modechoose.set('1')
moderadio = tk.Radiobutton(mode,variable=modechoose,value='1',text='转盘模式',bg='#B3B3B3').place(x=10,y=10)
moderadio = tk.Radiobutton(mode,variable=modechoose,value='2',text='磁带模式',bg='#B3B3B3').place(x=10,y=30)

#导入按钮
button = tk.Button(top,text="导入",width=171,height=83,command=imp).place(x=712,y=396)

top.mainloop()

```

  • 写回答

2条回答 默认 最新

  • MarkHan_ 2023-02-25 19:54
    关注

    该回答引用GPTᴼᴾᴱᴺᴬᴵ
    在你的代码中,输出的总是PY_VAR0和PY_VAR1是因为levelchoose和modechoose都是StringVar对象,而这两个对象的默认名称是PY_VAR0和PY_VAR1,所以输出的是默认名称。
    -
    如果你想输出单选框中选中的结果,可以修改imp()函数如下:

    def imp():
        print(levelchoose.get(), modechoose.get())
    

    使用get()方法获取StringVar对象的值,从而输出选中的难度和模式。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月6日
  • 已采纳回答 2月26日
  • 创建了问题 2月25日

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)