2017-03-18 29 views
0

我在爲我的網絡課程設計這個客戶端,它應該連接到我們的教授設置的MUD服務器,但是當我在Tkinter的入口小部件中遇到問題時,當按下「Enter」按鈕時,條目中的文本小部件應該從submit_value()函數打印到shell,但是我得到一個錯誤,說當從它調用.get()時,入口小部件不存在,任何人都可以幫我弄清楚這個錯誤嗎?爲什麼這個.get()在Tkinter中不起作用?

from tkinter import * 
from sys import exit 

def button_func(): 
     print("Test") 

def submit_value(): 
     print("Entered Value: %s" % (userEntry.get())) 

class TestClient(Frame): 
    def __init__(self, master): 
     Frame.__init__(self, master) 
     self.pack() 

     for n in range(3): 
      self.grid_rowconfigure(n, weight=1) 

     for n in range(8): 
      self.grid_columnconfigure(n, weight=1) 

     lb1 = Listbox(self, width=20,height=24) 
     lb1.insert(1,"WoW") 
     lb1.grid(row=0, column=0, columnspan=2, sticky='news') 

     t1 = Text(self, width=60) 
     t1.grid(row=0, column=3, columnspan=3) 

     lb2 = Listbox(self, width=20,height=24) 
     lb2.insert(1,"Hi") 
     lb2.grid(row=0, column=6, columnspan=2, sticky='news') 

     la1 = Label(self, text="Value entry:") 
     la1.grid(row=1, column=0) 

     userEntry = StringVar() 
     e1 = Entry(self, width=40, textvariable=userEntry) 
     e1.grid(row=1, column=1, columnspan=6) 

     e2 = Button(self, text="Enter", command=submit_value) 
     e2.grid(row=1, column=5, columnspan=10) 


     b1 = Button(self, text="Start", width=10,padx=10,pady=10, command=button_func) 
     b1.grid(row=2, column=0) 

     b2 = Button(self, text="Change Room", width=10,padx=10,pady=10, command=button_func) 
     b2.grid(row=2, column=3) 

     b3 = Button(self, text="FIGHT", width=10,padx=10,pady=10, command=button_func) 
     b3.grid(row=2, column=4) 

     b4 = Button(self, text="PvP FIGHT", width=10,padx=10,pady=10, command=button_func) 
     b4.grid(row=2, column=5) 

     b5 = Button(self, text="Loot", width=10,padx=10,pady=10, command=button_func) 
     b5.grid(row=2, column=6) 

     b6 = Button(self, text="Leave", width=10,padx=10,pady=10, command=button_func) 
     b6.grid(row=2, column=7) 

     stats = Listbox(self, width= 20) 
     stats.insert(1,"health:") 
     stats.grid(row=3, column=0, columnspan=8, sticky='news') 

root = Tk() 
root.title = "Test program" 
tw = TestClient(root) 
root.mainloop() 
+1

「*此錯誤*」...什麼錯誤? –

+0

您如何期待我們診斷我們無法看到的代碼? –

+0

所以對不起!,我的頭轉了一圈,一定是意外地發佈了沒有代碼的問題,對不起大家! – Will

回答

0

userEntryTestClient.__init__的局部變量。它不能在函數的範圍之外使用。您可以通過使用全局變量解決這個問題:

from tkinter import * 
from sys import exit 

userEntry = object 

def button_func(): 
    print("Test") 

def submit_value(): 
    global userEntry 
    print("Entered Value: %s" % (userEntry.get())) 

class TestClient(Frame): 
    def __init__(self, master): 
     global userEntry 
     Frame.__init__(self, master) 
     self.pack() 

     for n in range(3): 
      self.grid_rowconfigure(n, weight=1) 

     for n in range(8): 
      self.grid_columnconfigure(n, weight=1) 

     lb1 = Listbox(self, width=20,height=24) 
     lb1.insert(1,"WoW") 
     lb1.grid(row=0, column=0, columnspan=2, sticky='news') 

     t1 = Text(self, width=60) 
     t1.grid(row=0, column=3, columnspan=3) 

     lb2 = Listbox(self, width=20,height=24) 
     lb2.insert(1,"Hi") 
     lb2.grid(row=0, column=6, columnspan=2, sticky='news') 

     la1 = Label(self, text="Value entry:") 
     la1.grid(row=1, column=0) 

     userEntry = StringVar() 
     e1 = Entry(self, width=40, textvariable=userEntry) 
     e1.grid(row=1, column=1, columnspan=6) 

     e2 = Button(self, text="Enter", command=submit_value) 
     e2.grid(row=1, column=5, columnspan=10) 


     b1 = Button(self, text="Start", width=10,padx=10,pady=10, command=button_func) 
     b1.grid(row=2, column=0) 

     b2 = Button(self, text="Change Room", width=10,padx=10,pady=10, command=button_func) 
     b2.grid(row=2, column=3) 

     b3 = Button(self, text="FIGHT", width=10,padx=10,pady=10, command=button_func) 
     b3.grid(row=2, column=4) 

     b4 = Button(self, text="PvP FIGHT", width=10,padx=10,pady=10, command=button_func) 
     b4.grid(row=2, column=5) 

     b5 = Button(self, text="Loot", width=10,padx=10,pady=10, command=button_func) 
     b5.grid(row=2, column=6) 

     b6 = Button(self, text="Leave", width=10,padx=10,pady=10, command=button_func) 
     b6.grid(row=2, column=7) 

     stats = Listbox(self, width= 20) 
     stats.insert(1,"health:") 
     stats.grid(row=3, column=0, columnspan=8, sticky='news') 

root = Tk() 
root.title = "Test program" 
tw = TestClient(root) 
root.mainloop() 

上面的代碼建立userEntry作爲一個全局變量,因此它可以在任何地方在整個程序中使用。

+0

真棒謝謝你,我應該記得它是一個局部變量,在按下輸入按鈕之後,是否還有任何方法可以清除條目? – Will

+0

@將'userEntry.delete(0,END)'應該工作 –

+0

非常感謝主席先生,我很高興我推薦這個網站。 – Will