2017-01-12 15 views
-3

所以我剛剛拿起三天前的Python,我正在製作程序的過程。這也是我的第一次使用我的編程經驗。我花了不少不必要的時間去學習,到目前爲止我已經能夠抓住前進的方向,但恐怕我會陷入困境。錯誤拉tkinter條目控件字符串

我正在製作一個GUI來控制我創建的GPIO函數,它只是讓一對燈閃爍。目前它只是使兩個LED同時閃爍,用戶輸入延遲和用戶使用tkinter的Entry小部件輸入的循環數。這是我的問題出現的地方,當我嘗試從Entry小部件中獲取輸入時,出現'str'錯誤,這很有意義,因爲我假設條目是字符串數據類型,而且我的閃爍函數必須查找整數數據類型?但是,如果我強制這是一個整數,然後得到一個「無效的文字int()base 10」錯誤提示。請幫忙。

這是我目前的腳本:

聲明:可能有一些語法錯誤。迪爾的防火牆不允許我連接到網絡,因此我必須再次輸入所有這些信息。

import tkinter as tk 
root=tk.Tk() 
root.title("LED Controller") 
import sys 
import RPi.GPIO as GPIO 
import time 
GPIO.setwarnings(False) 
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(7, GPIO.OUT) 
GPIO.setup(11,GPIO.OUT) 

CycleLabel = tk.Label(root, text="Cycles", font="Verdana 12 bold") 
CycleLabel.grid(row=1,column=1) 
CycleInput = tk.Entry(root) 
CycleInput.grid(row=1,column=2) 
CycleInput.focus_set() 
cycle=CycleInput.get() 

SpeedLabel = tk.Label(root, text="Speed", font="Verdana 12 bold") 
SpeedLabel.grid(row=1,column=1) 
SpeedInput = tk.Entry(root) 
SpeedInput.grid(row=2,column=2) 
SpeedInput.focus_set() 
speed = SpeedInput.get() 

def callback(): 
    print (CycleInput.get()) 
    print (SpeedInput.get()) 

def Blink(cycle,speed): 
    for platypus in range (0, cycle):   ** This is the line that the error always points to. 
     print("Loop" + str(platypus+1)) 
     GPIO.output(7,True) 
     GPIO.output(11, True) 
     time.sleep(speed) 
     GPIO.output(7, False) 
     GPIO.output(11, False) 
     time.sleep(speed) 
     print("Done") 

ButtonFrame = tk.Frame(root) 
ButtonFrame.grid(row=3,column=1,columnspan=3) 

B2 = tk.Button(ButtonFrame, text="Start", width=10, command=lambda:Blink(cycle,speed), font="Verdana 10 bold") 
B2.grid(row=3,column=2) 

B1 = tk.Button(ButtonFrame, text"Print Inputs", width=10, command=callback, font="Verdana 10 bold") 
B1.grid(row=3,column=1) 

def clear(): 
    CycleInput.delete(0, 'end') 
    SpeedInput.delete(0, 'end') 

B3 = tk.Button(ButtonFrame, text="Clear", width=10, command=clear, font="Verdana 10 bold") 
B3.grid(row=3,column=3) 

CloseFrame = tk.Frame(root) 
CloseFrame.grid(row=4, column=1, columnsapn=3) 

B4 = tk.Button(CloseFrame, text="Close", width=20, command=root.destroy, font="Verdana 10 bold", activebackground='red') 
B4.grid(row=4,column=2) 
+0

您是否搜索了答案?你的問題沒有顯示任何研究的跡象。我建議你先搜索'[tkinter] gpio'。在我寫這篇文章時,有58個問題,其中一些可能與這個問題非常相似。 –

+0

我的問題甚至不涉及GPIO。 – Raudert

+0

感謝幫助! :) – Raudert

回答

1
command=lambda:Blink(cycle,speed) 

cyclespeed變量一次設置完成,形成相應的輸入字段之後。因此,他們總是空弦。用戶根本沒有機會爲他們輸入價值。您需要在點擊按鈕之後執行.get() s --無論是在此lambda中,還是在Blink本身中。

您還需要將輸入的字符串轉換爲整數,並處理輸入的字符串無效的可能性。