[英]Tkinter widget creation using Python3 of Rasberry Pi. Create an array of checkbuttons
我希望創建一個復選框數組並將其作為某種數組引用。 這使得使用較短的代碼塊編寫代碼變得容易得多。 理想的情況是這樣的
for IOBit in range(8)
self.GPIO_Array[IOBit] = tk.BooleanVar()
tk.Checkbutton(self.MyFrame , variable = self.GPIO_Array[IOBit] )
之后,我將得到一個由8個布爾變量組成的數組,稱為GPIO_Array[]
。 然后,我想使用諸如
self.GPIO_Array[Index].get()
關於如何執行此操作的任何想法可能是允許循環而不是一大堆半重復代碼的不同方法?
由於您沒有顯示實際的代碼,僅顯示“類似”的實際代碼,因此很難確定您做錯了什么。 這是一個工作示例,我嘗試模仿您想要的內容:
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.MyFrame = tk.Frame(self)
self.MyFrame.pack(side="top", fill="x")
self.GPIO_Array = []
for IOBit in range(8):
self.GPIO_Array.append(tk.IntVar())
w = tk.Checkbutton(self.MyFrame, variable=self.GPIO_Array[IOBit],
onvalue=1, offvalue=0, command=self.show)
w.pack(side="left")
self.label = tk.Label(self, text="", width=8)
self.label.pack(side="top", fill="x")
# show the current value when the GUI first starts
self.show()
def show(self):
s = ""
for IOBit in range(8):
s += str(self.GPIO_Array[IOBit].get())
self.label.configure(text=s)
if __name__ == "__main__":
root = tk.Tk()
app = Example(root)
app.pack(fill="both", expand=True)
root.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.