如何仅选择单选按钮值更改tkinter

时间:2017-05-10 10:36:10

标签: python tkinter radio-button

我有四个单选按钮和4个值。我想要它,以便当选择一个选定的单选按钮时,该值将从输入框中输入的值改变,但仅对所选的单选按钮改变,而其余按钮保持与它们之前的相同。

目前在我的代码中,所有值都更改为输入框中输入的值。

from tkinter import*

class NumberValues:
    def __init__(self, value):
        self.value = value

class NumberGUI:
    def __init__(self, parent):

        self.numbers = [NumberValues(4000), NumberValues(4000), NumberValues(8000), NumberValues(8000)]

        i_value = 0 #value given to radiobutton

        #set up for show radio buttons
        self.numChoice = IntVar() #instance for IntVar created
        self.numChoice.set(0) #sets the initial selection

        self.NumberList = []

        for num in self.numbers: #for loop outputs show info
            self.show_rb = Radiobutton(parent, variable = self.numChoice, value = i_value)
            self.show_rb.grid(row = i_value, column = 0)

            self.num_label = Label(parent, text = int(num.value))
            self.num_label.grid(row = i_value, column = 1)

            self.NumberList.append(self.num_label)

            i_value += 1

        self.quantity_entry = Entry(parent, width = 15)
        self.quantity_entry.grid(row = i_value + 1, column = 0)

        self.confirmOrder_btn = Button(parent, text = "Enter Number", command = self.calculate_num) 
        self.confirmOrder_btn.grid(row = i_value + 1, column = 1)


    def calculate_num(self):

        showNumberList = []

        placement = int(self.numChoice.get())

        for i in range(len(self.numbers)):
            number = int(self.quantity_entry.get())
            self.numbers[i].value = self.numbers[i].value - number
            avaiable = int(self.numbers[i].value)
            showNumberList.append(avaiable)
            self.NumberList[i].configure(text = showNumberList[i])

# main routine
if __name__ == "__main__":
    root = Tk()
    numberCaluclating = NumberGUI(root)
    root.mainloop()

如图所示,我创建了一个展示位置变量,指示应在我的列表中更改的值的位置。我想过在某种程度上使用它,但我尝试过的仍然会将它们全部更改为不正确的值。

在此不需要列表,我只是认为这是将所有值保持在一起并将它们配置在一起的可能方法。

那么当我选择一个选定的单选按钮时,如果只选择了那个值,而选择了单选按钮时其他值会改变,那该怎么办呢?

2 个答案:

答案 0 :(得分:0)

看看 - 我删除了一些逻辑,但GUI根据需要更新:

from tkinter import*

class NumberValues:
    def __init__(self, value):
        self.value = value

class NumberGUI:
    def __init__(self, parent):

        self.numbers = [NumberValues(4000), NumberValues(4000), NumberValues(8000), NumberValues(8000)]

        i_value = 0 #value given to radiobutton

        #set up for show radio buttons
        self.numChoice = IntVar() #instance for IntVar created
        self.numChoice.set(0) #sets the initial selection

        self.NumberList = []

        self.link = [] ###

        for num in self.numbers: #for loop outputs show info
            self.show_rb = Radiobutton(parent, variable = self.numChoice, value = i_value)
            self.show_rb.grid(row = i_value, column = 0)

            self.num_label = Label(parent, text = int(num.value))
            self.num_label.grid(row = i_value, column = 1)

            self.NumberList.append(self.num_label)

            self.link.append( (self.show_rb, self.num_label) ) ###

            i_value += 1

        self.quantity_entry = Entry(parent, width = 15)
        self.quantity_entry.grid(row = i_value + 1, column = 0)

        self.confirmOrder_btn = Button(parent, text = "Enter Number", command = self.calculate_num)
        self.confirmOrder_btn.grid(row = i_value + 1, column = 1)


    def calculate_num(self):
        placement = int(self.numChoice.get())
        ###
        self.link[placement][1].configure(text = int(self.quantity_entry.get()) )

# main routine
if __name__ == "__main__":
    root = Tk()
    numberCaluclating = NumberGUI(root)
    root.mainloop()

答案 1 :(得分:0)

您似乎在问如何设置一个值而不是所有值。除非我误解了这个问题,否则你需要做的就是删除循环。

例如:

def calculate_num(self):

    index = int(self.numChoice.get())
    number = int(self.quantity_entry.get())
    new_value = self.numbers[index].value - number
    self.numbers[index].value = new_value
    self.NumberList[index].configure(text=new_value)