单选按钮tkinter python

时间:2016-05-15 23:31:36

标签: python tkinter radio-button

我遇到一个问题,当我尝试点击其中一个时,我的所有单选按钮都被选中了。这是一个转换计算器,所以一旦我解决了这个问题,我将能够为我的其他转换携带相同的代码。非常感谢任何帮助。

谢谢,

杰米

`from tkinter import*
from tkinter import ttk

class GUI:
    def __init__(self, root):
        notebook = ttk.Notebook(root)
        notebook.pack()

        self.temp_frame = ttk.Frame(notebook)
        self.length_frame = ttk.Frame(notebook)
        self.weight_frame = ttk.Frame(notebook)

#-----------------Length------------------------#
        notebook.add(self.length_frame, text = "Length")                     

        #Radio Buttons
        v = StringVar()
        MODES = ["mm","cm","Inch","Feet","Yards","Metre","Km","Miles"]

        v.set("0") # initialize
        r=0
        for r in range(len(MODES)):
            b = ttk.Radiobutton(self.length_frame, text=MODES[r], variable=v )
            b.grid(row=r ,column = 0, sticky = W)



        #Radio Buttons
        v1 = StringVar()
        MODES1 = ["mm","cm","Inch","Feet","Yards","Metre","Km","Miles"]

        v1.set("0")#initialize
        r=0
        for r in range(len(MODES1)):
            b = ttk.Radiobutton(self.length_frame, text=MODES1[r], variable=v1 )
            b.grid(row=r ,column = 6, sticky = W)




        #Entry Box
        self.Text_length_left = StringVar()
        self.entry_length_left = ttk.Entry(self.length_frame, textvariable = self.Text_length_left, width = 15)
        self.entry_length_left.grid(row = 4, column = 2)

        self.Text_length_right = StringVar()
        self.entry_length_right = ttk.Entry(self.length_frame, textvariable = self.Text_length_right, width = 15, state = "readonly")
        self.entry_length_right.grid(row = 4, column = 4)

        #Label
        self.label_3 = Label(self.length_frame, text = "From:")
        self.label_3.grid(row = 3, column = 2)

        self.label_4 = Label(self.length_frame, text = "To:")
        self.label_4.grid(row = 3, column = 4)

        self.label_1 = Label(self.length_frame, text = "-->")
        self.label_1.grid(row = 4, column = 3)

        self.label_2 = Label(self.length_frame, text = " ")
        self.label_2.grid(row = 4, column = 5)

#---------------------Temp Frame ----------------------#
        notebook.add(self.temp_frame, text = "Temperature")


if __name__ == "__main__":
    root = Tk()
    app = GUI(root)
    root.mainloop()`

1 个答案:

答案 0 :(得分:3)

您从未设置value关键字。这是单击时一组radiobutton的控制变量中存储的内容。