tkin中单选按钮的默认选择

2024-09-29 22:26:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码。预期的行为是在一组单选按钮中设置默认选项。我在其他地方使用了非常相似的代码,但在这个例子中,最后3个选项都显示为选中。我是新来的,一辈子都看不出哪里出了问题。非常感谢你的帮助。在

charttypevar=StringVar()

chart_type_1 = ttk.Radiobutton(chart_type_frame, text="3d with catagories",variable=charttypevar,value='3d with catagories')#
chart_type_2 = ttk.Radiobutton(chart_type_frame, text="3d no catagories",variable=charttypevar,value='3d no catagories')#
chart_type_3 = ttk.Radiobutton(chart_type_frame, text="3d relief plot", variable=charttypevar, value='3d relief plot')  #
chart_type_4=ttk.Radiobutton(chart_type_frame,text="Not yet defined",variable=charttypevar,value="Not yet defined")
charttypevar.set('Not yet defined')  # intended to be set as default

chart_type_1.grid(column=0,row=1,sticky="W")
chart_type_2.grid(column=0,row=2,sticky="W")
chart_type_3.grid(column=0,row=3,sticky="W")
chart_type_4.grid(column=0,row=4,sticky="W")

Tags: textvaluetypechartnotcolumnvariableframe
1条回答
网友
1楼 · 发布于 2024-09-29 22:26:37

您没有共享完成的代码。但这解决了你的问题。什么是“ttk”?如果您将tkinter作为“ttk”导入,那么在定义stringvar时为什么不使用它呢?在

import tkinter as ttk
chart_type_frame=Tk()
charttypevar=ttk.StringVar()

chart_type_1 = ttk.Radiobutton(chart_type_frame, text="3d with catagories",variable=charttypevar,value='3d with catagories')#
chart_type_2 = ttk.Radiobutton(chart_type_frame, text="3d no catagories",variable=charttypevar,value='3d no catagories')#
chart_type_3 = ttk.Radiobutton(chart_type_frame, text="3d relief plot", variable=charttypevar, value='3d relief plot')  #
chart_type_4=ttk.Radiobutton(chart_type_frame,text="Not yet defined",variable=charttypevar,value="Not yet defined")
charttypevar.set('Not yet defined')  # intended to be set as default

chart_type_1.grid(column=0,row=1,sticky="W")
chart_type_2.grid(column=0,row=2,sticky="W")
chart_type_3.grid(column=0,row=3,sticky="W")
chart_type_4.grid(column=0,row=4,sticky="W")

相关问题 更多 >

    热门问题