1
我試圖構建一個用於與串行設備進行通信的gui。爲此,我使用了Tkinter。我的問題是,每次執行腳本時,只執行estCon函數和mainloop,因此gui永遠不會啓動。如果我在主循環之後放置了estCon函數的定義,它說沒有找到estCon函數。在主循環之前執行Python Tkinter回調函數
def estCon():
# establish connection
while True:
try:
ser = serial.Serial(port, baud, bytesize)
print('Connected.')
break
except serial.SerialException:
print('waiting for device ' + port + ' to be available.')
time.sleep(3)
starttime = time.time()
outfile = open(filename, 'a')
doprint = True
root = Tk()
estConButton = Button(root, text="Establish serial connection",
command=estCon())
estConButton.pack()
root.mainLoop()
那麼它打印出來什麼或只是坐在那裏? – tacaswell 2013-04-06 00:40:38
jap,estCon函數立即執行,它正在打印「等待設備」表達式 – user2251084 2013-04-06 00:45:42