如何从单选按钮中获取值以生成if语句

时间:2014-11-10 12:30:59

标签: python tkinter

from tkinter import *
import tkinter.messagebox
from ProjectHeader import *


class Gui(object):
    def __init__(self, parent):

        self.gui = parent

        self.gui.geometry("350x200")

        self.gui.title("Converter")

        self.checked1 = IntVar()
        self.checked2 = IntVar()

        self.c1 = Radiobutton(self.gui, text='(1)Centimeters', variable=self.checked1, value = 1)
        self.c2 = Radiobutton(self.gui, text='(2)Meter', variable=self.checked1, value = 2)
        self.c3 = Radiobutton(self.gui, text='(3)Millimeters', variable=self.checked1, value = 3)
        self.c4 = Radiobutton(self.gui, text='(4)Kilometers', variable=self.checked1, value = 4)

        self.c5 = Radiobutton(self.gui, text='(5)Centimeters', variable=self.checked2, value = 5)
        self.c6 = Radiobutton(self.gui, text='(6)Meter', variable=self.checked2, value = 6)
        self.c7 = Radiobutton(self.gui, text='(7)Millimeters', variable=self.checked2, value = 7)
        self.c8 = Radiobutton(self.gui, text='(8)Kilometers', variable=self.checked2, value = 8)

        self.b1 = Button(self.gui, text="Convert", command=self.callback)
        self.l1 = Label(self.gui, text="Value")
        self.l2 = Label(self.gui, text="Convert ->")
        self.e1 = Entry(self.gui, bd = 5)

        self.e1.insert(0, "0")

        self.c1.pack(side = BOTTOM)
        self.c2.pack(side = BOTTOM)
        self.c3.pack(side = BOTTOM)
        self.c4.pack(side = BOTTOM)
        self.c5.pack(side = BOTTOM)
        self.c6.pack(side = BOTTOM)
        self.c7.pack(side = BOTTOM)
        self.c8.pack(side = BOTTOM)

        self.l1.pack(side = TOP)
        self.l2.pack(side = TOP)
        self.e1.pack(side = TOP)
        self.b1.pack(side = TOP)

        self.c1.place(x = 3, y = 90)
        self.c2.place(x = 3, y = 110)
        self.c3.place(x = 3, y = 130)
        self.c4.place(x = 3, y = 150)
        self.c5.place(x = 235, y = 90)
        self.c6.place(x = 235, y = 110)
        self.c7.place(x = 235, y = 130)
        self.c8.place(x = 235, y = 150)

        self.l2.place(x = 140, y = 110)



    def callback(self):


        if self.e1.get() == "0":
            messagebox.showinfo("Error", "Please enter a value")

        elif self.checked1.get(): #Centiemters
            C = Centimeters(self.e1.get())
            if self.checked2.get():
                messagebox.showinfo("Error", "Converting the same unit!")
            if self.checked2.get():
                value = C.ToMeters()
                messagebox.showinfo("Value", str(value) + ' cm')
            if self.checked2.get():
                value = C.ToMillimeters()
                messagebox.showinfo("Value", str(value) + ' cm')
            if self.checked2.get():
                value = C.ToKilometers()
                messagebox.showinfo("Value", str(value) + ' cm')

root = Tk()

my_window = Gui(root)

root.mainloop()

所以现在我将self.checked1.get() == 1更改为self.checked1.get() 因为你通过检查radiobutton说,它会选择选项

但是现在,当我检查(1)厘米按钮,或者(5)厘米到(8)公里按钮时,程序会选择所有选项

例如,如果我选中(1)厘米按钮和(8)公里,程序不仅会弹出公里的转换值,还会弹出米的转换值,毫米。

所以我的问题是如何修复程序,当我选中左侧的第一个方框时,右侧的其中一个方框,程序会给出正确的选择?

这是checkedbutton的界面 http://postimg.org/image/6uqwkybw5/

1 个答案:

答案 0 :(得分:0)

如果我理解你想要什么,你应该使用以下代码。使用self.checked1.get()查看原始单位是什么,并使用self.checked2.get()查看必须转换的单位。

    if self.e1.get() == "0":
        messagebox.showinfo("Error", "Please enter a value")

    elif self.checked1.get() == 1: #Centimeters
        C = Centimeters(self.e1.get())
        if self.checked2.get() == 5:
            messagebox.showinfo("Error", "Converting the same unit!")
        if self.checked2.get() == 6:
            value = C.ToMeters()
            messagebox.showinfo("Value", str(value) + ' m')
        if self.checked2.get() == 7:
            value = C.ToMillimeters()
            messagebox.showinfo("Value", str(value) + ' mm')
        if self.checked2.get() == 8:
            value = C.ToKilometers()
            messagebox.showinfo("Value", str(value) + ' km')