【问题标题】:Genericaly retrieve text value from radio button通常从单选按钮检索文本值
【发布时间】:2015-04-20 16:11:04
【问题描述】:

我希望能够从单选按钮检索文本值。

例如:

first  = Radiobutton(frame, text=name, variable=V, value=0)
second = Radiobutton(frame, text=name, variable=V, value=1)
third  = Radiobutton(frame, text=name, variable=V, value=3)

buttonPressed = V.get()

这会给我价值,但我想弄清楚如何获得 文本值。因为我的按钮是从通用属性列表创建的 来自 XML 文档。

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    要获取小部件的属性值,请使用cget 方法:

    first_text = first.cget("text")
    

    【讨论】:

      【解决方案2】:

      您不需要使用IntVar,而是使用StringVar

      >>> root=tkinter.Tk()
      >>> V=tkinter.StringVar()
      >>> name = "abc"
      >>> first = tkinter.Radiobutton(root, text=name, variable=V, value=name)                                                      
      >>> first.pack()                                                                                                               
      >>> V.get()
      'abc'                                                                                                                          
      >>>
      

      这样,V.get() 与所选Radiobuttontext 相同。

      另一种方法是列出Radiobuttons,例如[first, second, third]。然后你可以通过radiobuttons[V.get()]["text"]找到text,其中变量VIntVar()V = IntVar())初始化,因为Radiobutton可以像字典一样访问(相当于config)检索text 值。但是,这确实涉及维护列表。

      【讨论】:

      • 非常感谢。我正在努力找到像上面这样的方法(radiobuttons[V.get()]["text"]
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多