Radiobutton修改spinbox Python中的增量值

时间:2013-12-07 03:07:04

标签: python python-2.7 tkinter radio-button increment

我正在尝试使用Python 2.7Tkinter中创建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 

1 个答案:

答案 0 :(得分:1)

嗨,我刚刚找到了办法。我必须更改class中可用的对象,将其添加到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)

然后在call函数中,我使用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 解决方案,我们非常感谢!干杯!