3
我正在從串行端口記錄數據到.txt文件的Python程序。該程序使用Tkinter的OptionMenu來詢問用戶使用什麼串口。端口的列表由如下:填充Tkinter OptionMenu與未知長度的列表,即串行端口列表
def serial_ports():
for port in list_ports.comports():
yield port
OPTIONS = list(serial_ports())
然後該程序使OptionMenu(窗口名稱=「雙贏」,幀=「C」):
var = StringVar(win)
var.set(OPTIONS[0]) # initial value
for item in OPTIONS:
print item #statement here is temporary to prevent the prog from giving error while testing
w = apply(OptionMenu, (c, var, item))
w.pack(side=RIGHT)
我得到的物品打印隨後,但我找不到一種方法讓他們進入OptionMenu。代碼如:
for n in OPTIONS:
#tried different things here: count, n = item, et whatever crazy stuff one tries.
w = apply(OptionMenu, (c, var, OPTIONS[n]))
沒有解決問題。
最後的想法是,OptionMenu的選擇返回串行端口的名稱,最好是一個字符串(而不是索引)。 Wich將被插入:
ser0 = serial.Serial(port = '[HERE!!!]', baudrate = 9600, timeout = 0.5)
ps。目前該程序是爲Python 2.7.1的Mac OS X編寫的。
你的第一個建議爲我工作。使用課堂我覺得有點難以理解(我是一個相當新手)。但我會嘗試。謝謝。 – Hydrographer