2013-07-16 46 views
0

試圖進入Python,我很確定答案就在我面前。但我不確定如何實現他人的想法。單擊按鈕後,無法使用字符串更新標籤

我有一本書籍清單,每本書都有一個清單,包含標題,期望清單和當前清單的兩種翻譯。我想要一個窗口彈出,然後點擊一個按鈕後,窗口將顯示書籍清單的摘要。 (目前有4個)。

我可以把它打印出來,但沒有線索知道如何獲取GUI消息。另外,當我沒有在兩個地方定義invOutput時,我會得到錯誤...這看起來不正確。如果只是裏面的功能,它說,它是沒有定義的,如果它只有外面我得到UnboundLocalError: local variable 'invOutput' referenced before assignment

from Tkinter import * 

#These are all the books sorted in lists. 
books = [ #English Title, Spanish Title, Desired Stock, Current Stock 
["Bible", "Biblia", 10, 5], 
["Bible Teach", "Bible Teach", 10, 10], 
["Song Book", "El Song Book", 10, 10], 
["Daniel's Prophecy", "Spanish D Prof", 10, 10] 
] 

invOutput = "" 

def inventoryButton(): 
     invOutput = "" 
     for book in books: 
       if book[2] > book[3]: 
        invOutput += "Title: " + book[0] + "\n" 
        invOutput += "You need to order more books.\n\n" 
       else: 
        invOutput += "Title: " + book[0] + "\n" 
        invOutput += "Status: Inventory is sufficient.\n\n" 
     print invOutput 

############## 

window = Tk() 
window.title("Literature Inventory System") 
window.geometry("500x500") 

button = Button(window, text="Check Inventory", command=inventoryButton) 
button.pack() 

summary = Label(window, textvariable=invOutput) 
summary.pack() 

window.mainloop() 


############## 
+0

如果其他人的代碼讓你感到恐慌並且沒有幫助,那麼在這裏可能會有什麼好的答案?在學習編寫代碼之前,你必須學會​​閱讀代碼;這是沒有辦法的。 – abarnert

回答

0

正如我理解你的問題,你想利用正在打印在終端和什麼在GUI中顯示它的權利?如果是這樣,這將做到這一點:

from Tkinter import * 


books = [ 
["Bible", "Biblia", 10, 5], 
["Bible Teach", "Bible Teach", 10, 10], 
["Song Book", "El Song Book", 10, 10], 
["Daniel's Prophecy", "Spanish D Prof", 10, 10] 
] 

def inventoryButton(): 
    invOutput = "" 
    for book in books: 
     if book[2] > book[3]: 
      invOutput += "Title: " + book[0] + "\n" 
      invOutput += "You need to order more books.\n\n" 
     else: 
      invOutput += "Title: " + book[0] + "\n" 
      invOutput += "Status: Inventory is sufficient.\n\n" 
    #Set the text of the Label "summary" 
    summary["text"] = invOutput 

window = Tk() 
window.title("Literature Inventory System") 
window.geometry("500x500") 

button = Button(window, text="Check Inventory", command=inventoryButton) 
button.pack() 

summary = Label(window) 
summary.pack() 

window.mainloop() 

當您單擊「檢查庫存」,終端中打印的所有內容現在顯示在GUI中。

+0

非常感謝! – theamazingaustin

+0

這是令人尷尬的簡單。 – theamazingaustin