2017-05-10 79 views
0

我有四個單選按鈕和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() 

如圖所示,我創建了一個位置變量,指示應該在列表中更改值的位置。我想在某種程度上使用它,但我所嘗試的仍然將它們全部更改爲不正確的值。

列表不需要在這裏,我只是認爲這是一個可能的方法來保存所有的值並將它們配置在一起。

那麼我怎樣才能讓選定的單選按鈕被選中,只有該值發生變化,而其他值在其單選按鈕被選中時會發生變化?

+0

一種方法是檢查一次輸入數字,單選按鈕的狀態,並且只更新相應的標籤。但爲了做到這一點,你需要保持對單選按鈕的引用。 – handle

回答

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() 
+0

與我對這個問題的評論相反,選擇(單選按鈕)已經可以作爲'placement'來使用,因此可以用作索引來訪問相應的Label(我在'self.link'中引用它)。 – handle

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)