Python Tkinter 单选按钮变量输出
Python Tkinter Radiobutton variable output
我试图找到一种方法来从 Radiobutton 中获取值,将其设置为变量,并在代码的其他部分使用 class 之外的变量。这是我的代码示例:
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" 设置为用户选择的值?然后在 class.
之外使用该值
我尝试使用 Method=self.v1.get() 但没有用。
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)
root = Tk()
w = Window(root)
w.master.mainloop()
print "The radiobutton outside of the class: %d" %w.v1.get()
这是我在评论中的意思的一个例子。当您关闭 window 时,我的示例应该打印当前选择的 Radiobutton
的 value
。
在您的情况下,根据 Radiobutton
具有的任何值,您将决定调用哪些函数。
我试图找到一种方法来从 Radiobutton 中获取值,将其设置为变量,并在代码的其他部分使用 class 之外的变量。这是我的代码示例:
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" 设置为用户选择的值?然后在 class.
之外使用该值我尝试使用 Method=self.v1.get() 但没有用。
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)
root = Tk()
w = Window(root)
w.master.mainloop()
print "The radiobutton outside of the class: %d" %w.v1.get()
这是我在评论中的意思的一个例子。当您关闭 window 时,我的示例应该打印当前选择的 Radiobutton
的 value
。
在您的情况下,根据 Radiobutton
具有的任何值,您将决定调用哪些函数。