2017-04-16 100 views
1

我必須創建多個單選按鈕。每個網格上都有自己的名稱和位置。還有幾個變量涉及。如何使用循環在Python中創建單選按鈕

我存儲所有的數據在一個元組 創建這些單選按鈕(我知道FONTS不會造成問題,它的存儲上面這一點,但這裏沒有顯示):

self.unitType = IntVar() 
    self.matPropSel = IntVar() 
    self.secProfSel = IntVar() 
    self.endSupSel = IntVar() 
    self.loadTypeSel = IntVar() 

    self.RADIOLABELS = ( # Text, Font, Variable, Value, Row, Column 
     ('English', self.FONTS[3], self.unitType, 1, 3, 1), 
     ('metric', self.FONTS[3], self.unitType, 2, 4, 1), 
     ('select preset', self.FONTS[3], self.matPropSel, 1, 6, 1), 
     ('manual entry', self.FONTS[3], self.matPropSel, 2, 7, 1), 
     ('select preset', self.FONTS[3], self.secProfSel, 1, 10, 1), 
     ('manual entry', self.FONTS[3], self.secProfSel, 2, 11, 1), 
     ('one end', self.FONTS[3], self.endSupSel, 1, 15, 1), 
     ('both ends', self.FONTS[3], self.endSupSel, 2, 16, 1), 
     ('point', self.FONTS[3], self.loadTypeSel, 1, 18, 1), 
     ('uniform distribution', self.FONTS[3], self.loadTypeSel, 2, 19, 1), 
     ('uniform variation', self.FONTS[3], self.loadTypeSel, 3, 20, 1) 
    ) 

所以,我將如何使用for循環來遍歷這個元組,並從每一行生成一個單選按鈕?所有的變量都必須相同嗎?我遇到了問題。

這是我在一個循環的嘗試:

outerIndexer = 0 
    for labels in self.RADIOLABELS: 
     Radiobutton(self, text=self.RADIOLABELS[outerIndexer][0], font=self.RADIOLABELS[outerIndexer][1], 
        variable=self.RADIOLABELS[outerIndexer][2], value=self.RADIOLABELS[outerIndexer][3])\ 
      .grid(row=self.RADIOLABELS[outerIndexer][4], column=self.RADIOLABELS[outerIndexer][5]) 
    outerIndexer += 1 
+0

你能詳細說明究竟是什麼,你不明白?或者在for循環中添加你自己的嘗試,以便有人可以幫助你並糾正你可能有的一些可能的疑問。 –

+0

@Ashish Nitin Patil肯定 – thisissparzo

回答

1

您可以通過放射性標記循環不喜歡這樣。注意:還建議將按鈕保存到列表中,以免丟失。

self.RADIOLABELS = ( # Text, Font, Variable, Value, Row, Column 
    ('English', self.FONTS[3], self.unitType, 1, 3, 1), 
    ('metric', self.FONTS[3], self.unitType, 2, 4, 1), 
    ('select preset', self.FONTS[3], self.matPropSel, 1, 6, 1), 
    ('manual entry', self.FONTS[3], self.matPropSel, 2, 7, 1), 
    ('select preset', self.FONTS[3], self.secProfSel, 1, 10, 1), 
    ('manual entry', self.FONTS[3], self.secProfSel, 2, 11, 1), 
    ('one end', self.FONTS[3], self.endSupSel, 1, 15, 1), 
    ('both ends', self.FONTS[3], self.endSupSel, 2, 16, 1), 
    ('point', self.FONTS[3], self.loadTypeSel, 1, 18, 1), 
    ('uniform distribution', self.FONTS[3], self.loadTypeSel, 2, 19, 1), 
    ('uniform variation', self.FONTS[3], self.loadTypeSel, 3, 20, 1) 
) 

radiobuttons = [] 
for _Text, _Font, _Variable, _Value, _Row, _Column in self.RADIOLABELS: # it will unpack the values during each iteration 
    _Radio = tk.Radiobutton(root, text = _Text, font = _Font, variable = _Variable, value = _Value) 
          #^root should be the frame/Tk you're trying to place it on, it will be self if this is a direct subclass of a Frame or Tk 
      #^or Radiobutton(.....) if you did from tkinter import * 
    _Radio.grid(row = _Row, column = _Column) 
    radiobuttons.append(_Radio) 
+0

這絕對讓我更接近我需要的地方。雖然,我仍然得到一個self._root = master._root() AttributeError:'NoneType'對象沒有屬性'_root'我應該以不同的方式聲明這些變量嗎?我認爲問題與他們有關,因爲我用同樣的方式製作了一堆標籤,他們很好。 @abccd – thisissparzo

+0

用你的master/frame/Tk被調用的任何東西替換'root'。你從來沒有指定它,所以我只是用默認的'root'去了。如果你從'tkinter import *'做'',我會建議你小寫變量名,所以它不會搞砸。 – abccd

+0

是的,我已經改變了這個'自我',如果我使用它來繼承,應該是Tk嗎? @abccd – thisissparzo