我有四個單選按鈕和4個值。我希望這樣,當選擇一個選定的單選按鈕時,該值將從在輸入框中輸入的值改變,但僅限於所選的單選按鈕,而其餘部分保持與他們當前的相同。如何僅使選定的單選按鈕值更改tkinter
當前在我的代碼中,所有值都變爲在輸入框中輸入的值。
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()
如圖所示,我創建了一個位置變量,指示應該在列表中更改值的位置。我想在某種程度上使用它,但我所嘗試的仍然將它們全部更改爲不正確的值。
列表不需要在這裏,我只是認爲這是一個可能的方法來保存所有的值並將它們配置在一起。
那麼我怎樣才能讓選定的單選按鈕被選中,只有該值發生變化,而其他值在其單選按鈕被選中時會發生變化?
一種方法是檢查一次輸入數字,單選按鈕的狀態,並且只更新相應的標籤。但爲了做到這一點,你需要保持對單選按鈕的引用。 – handle