Python Tkinter:如何设置 ttk.Radiobutton 激活并获取其值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18708172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:32:49  来源:igfitidea点击:

Tkinter: How to set ttk.Radiobutton activated and get its value?

pythonpython-2.7tkinterttk

提问by minerals

1) I need to set one of my three ttk.Radiobuttons activated by default when I start my gui app.
How do I do it?

1)当我启动我的 gui 应用程序时,我需要设置我的三个 ttk.Radiobuttons 中的一个默认激活。
我该怎么做?

2) I also need to check if one of my ttk.Radiobuttons was activated/clicked by the user.
How do I do it?

2)我还需要检查我的 ttk.Radiobuttons 之一是否被用户激活/点击。
我该怎么做?

rb1 = ttk.Radiobutton(self.frame, text='5', variable=self.my_var, value=5)
rb2 = ttk.Radiobutton(self.frame, text='10', variable=self.my_var, value=10)
rb3 = ttk.Radiobutton(self.frame, text='15', variable=self.my_var, value=15)
self.rb1.grid(row=0)
self.rb2.grid(row=1)
self.rb3.grid(row=2)

采纳答案by Alok

use self.my_var.set(1)to set the radiobutton with text='5'as the default RadioButton.

用于self.my_var.set(1)text='5'单选按钮设置为默认 RadioButton。

To get the selected one you have to call a function

要获得选定的一个,您必须调用一个函数

rb1 = ttk.Radiobutton(self.frame, text='5', variable=self.my_var, value=5,command=self.selected)
rb2 = ttk.Radiobutton(self.frame, text='10', variable=self.my_var, value=10,command=self.selected)
rb3 = ttk.Radiobutton(self.frame, text='15', variable=self.my_var, value=15,command=self.selected)
self.rb1.grid(row=0)
self.rb2.grid(row=1)
self.rb3.grid(row=2)

def selected(self):
     if self.my_var.get()==5:
        "do something"
     elif self.my_var.get()==10:
        "do something"
     else:
        "do something"

回答by Aaditya M Nair

You can use rb1.state['selected']to set the default as rb1and self.my_var.get()to get the value (i.e the text variable) of the radiobutton.

您可以使用rb1.state['selected']将默认值设置为rb1self.my_var.get()获取单选按钮的值(即文本变量)。

回答by fbmd

As for your first question, a convenient and straightforward way is to call the invokemethod:

至于你的第一个问题,一个方便直接的方法是调用invoke方法:

rb2.invoke()

Note that this also runs any commandassociated with this button.

请注意,这也会运行command与此按钮相关的任何内容。

See the Ttk::radiobutton invoke documentation.

请参阅Ttk::radiobutton 调用文档

回答by Sébastien S.

I'm just adding something to Gogo's answer but i can't comment because i don't have enough reputation.

我只是在 Gogo 的回答中添加了一些内容,但我无法发表评论,因为我没有足够的声誉。

Add self.my_var=tk.IntVar()before the radiobuttons or your program will not know the variable. see The Variable Classesfor more informations

self.my_var=tk.IntVar()在单选按钮之前添加,否则您的程序将不知道该变量。有关更多信息,请参阅变量类

for exemple, in my case i need a StringVar(): this works for python 3.* change tkinterto Tkinterfor python 2 (i'm not 100% sure tho)

对于为例,在我来说,我需要一个STRINGVAR():该作品蟒蛇3. *的变化tkinter,以Tkinter用于Python 2(我不是100%肯定寿)

import tkinter as tk
from tkinter import ttk

class XFile_Page(tk.Frame):
    ...

    self.joinType = tk.StringVar()
    self.rbLeft = ttk.Radiobutton(self.frameRadioButtons, text='left', variable=self.joinType, value='left',command=self.RadioBtnSelected)
    self.rbRight = ttk.Radiobutton(self.frameRadioButtons, text='right', variable=self.joinType, value='right',command=self.RadioBtnSelected)

    self.rbLeft.grid(row=0)
    self.rbRight.grid(row=1)

    #select the rbLeft radiobutton
    self.rbLeft.invoke()

    def RadioBtnSelected(self):
        print(self.joinType.get())