Radiobutton在spinbox Python中修改增量值
我正在尝试使用Radiobutton在spinbox Python中修改增量值,python,python-2.7,tkinter,radio-button,increment,Python,Python 2.7,Tkinter,Radio Button,Increment,我正在尝试使用Tkinter在python2.7中创建GUI。我需要将我的Spinbox中的增量值相应地更改为从选择的Radiobutton中选择的值。我尝试过不同的方法,但至今没有成功。我正在附加一部分不起作用的代码。如果有人能想出解决办法,我真的很感激。谢谢 class Frequency_Window(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.
Tkinter
在python2.7
中创建GUI。我需要将我的Spinbox
中的增量值相应地更改为从选择的Radiobutton
中选择的值。我尝试过不同的方法,但至今没有成功。我正在附加一部分不起作用的代码。如果有人能想出解决办法,我真的很感激。谢谢
class Frequency_Window(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
global var_increment
self.var_x = IntVar()
self.self_value = IntVar()
self.freq_Hz = IntVar()
self.parent.title("Test")
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(1, pad=7)
self.rowconfigure(5, weight=1)
self.rowconfigure(5, pad=7)
self.var_x.set("1")
label_RadioButton = ttk.Label(self,
justify=LEFT,
text="Increments")
label_RadioButton.grid(sticky=N+W+S+E,
pady=4,
padx=5,
row=0,
column=1)
R1 = ttk.Radiobutton(self,
text="1",
variable=self.var_x,
command=self.set_increment,
value=1)
R1.grid(sticky=NW,
row=1,
column=1,
pady=5)
R10 = ttk.Radiobutton(self,
text="10",
variable=self.var_x,
command=self.set_increment,
value=10)
R10.grid(sticky=NW,
row=2,
column=1,
pady=5)
R100 = ttk.Radiobutton(self,
text="100",
variable=self.var_x,
command=self.set_increment,
value=100)
R100.grid(sticky=NW,
row=3,
column=1,
pady=5)
var_freq_Hz = Spinbox(self,
textvariable=self.freq_Hz,
font=("Calibri",30),
justify="right",
bd=0,
bg='black',
fg='white',
from_=0,
to=999,
increment=self.var_x.get(),
width=4,
relief=FLAT,
buttonbackground='black')
var_freq_Hz.grid(sticky=N+W+S+E,
row=1,
column=0,
columnspan=1,
rowspan=1,
padx=5)
def set_increment(self):
selection = "You selected the option " + str(self.var_x.get())
print selection
return
嗨,我刚找到办法。我必须更改
类中可用的对象,将其添加到self
。由此:
var_freq_Hz = Spinbox(self,
textvariable=self.freq_Hz,
font=("Calibri",30),
justify="right",
bd=0,
bg='black',
fg='white',
from_=0,
to=999,
increment=self.var_x.get(),
width=4,
relief=FLAT,
buttonbackground='black')
var_freq_Hz.grid(sticky=N+W+S+E,
row=1,
column=0,
columnspan=1,
rowspan=1,
padx=5)
为此:
self.var_freq_Hz = Spinbox(self,
textvariable=self.freq_Hz,
font=("Calibri",30),
justify="right",
bd=0,
bg='black',
fg='white',
from_=0,
to=999,
width=4,
relief=FLAT,
buttonbackground='black')
self.var_freq_Hz.grid(sticky=N+W+S+E,
row=1,
column=0,
columnspan=1,
rowspan=1,
padx=5)
然后在调用函数中,我使用configure选项更改increment的值,如下所示:
def set_increment(self):
selection = "You selected the option " + str(self.var_x.get())
self.var_freq_Hz.config(increment = self.var_x.get())
print selection
return
现在它工作正常了。然而,如果有人提出了一个更具Pythonic的解决方案,我们将不胜感激!干杯