【发布时间】:2015-09-11 00:07:56
【问题描述】:
我正在尝试找到一种方法来从 Radiobutton 中获取值,将其设置为变量,然后在代码的其他部分中使用类外部的变量。这是我的代码示例:
from Tkinter import *
class Window():
def __init__(self, master):
self.master = master
self.v1 = IntVar()
Label(master, text="""Which Method?""",justify = LEFT, padx = 20).pack()
Radiobutton(master, text="Positive",padx = 20, variable=self.v1, value=1).pack(anchor=W)
Radiobutton(master, text="Negative", padx = 20, variable=self.v1, value=2).pack(anchor=W)
Radiobutton(master, text="Both", padx = 20, variable=self.v1, value=3).pack(anchor=W)
有没有办法,最好不使用定义和命令选项,将变量“方法”设置为用户选择的值?然后在类外使用该值。
我尝试使用 Method=self.v1.get() 但没有奏效。
【问题讨论】:
-
你不能在课堂外使用
w.v1.get()访问它吗? (假设w是Window实例的名称) -
“那行不通”是什么意思?发生了什么?这正是您从
IntVar获取值的方式。
标签: python variables tkinter radio-button