2013-04-06 109 views
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() 
+0

那麼它打印出來什麼或只是坐在那裏? – tacaswell 2013-04-06 00:40:38

+0

jap,estCon函數立即執行,它正在打印「等待設備」表達式 – user2251084 2013-04-06 00:45:42

回答

3

你需要改變這一行:

estConButton = Button(root, text="Establish serial connection", command=estCon()) 

要:

estConButton = Button(root, text="Establish serial connection", command=estCon) 

通知缺少括號()的。基本上,當你按下按鈕而不是實際的調用時,你需要傳遞一個函數的引用。

+0

thnx,這有助於! – user2251084 2013-04-06 00:51:29

相關問題