2016-03-16 156 views
0

我在製作GUI程序時得到了TypeErrorTypeError:send()缺少2個必需的位置參數:'interp'和'cmd'

下面是代碼:

import time, random 
from tkinter import * 

class Application(Frame): 
    def __init__(self, master): 
     Frame.__init__(self, master) 
     self.grid() 
     self.create_widgets() 

    def create_widgets(self): 
     self.lbl1=Label(self, text="Write your message:") 
     self.lbl1.grid(row=0, column=0, columnspan=2, sticky=W) 

     self.entry=Entry(self) 
     self.entry.grid(row=0, column=2, columnspan=3, sticky=W) 

     self.bttn=Button(self, text="Send", command=self.send) 
     self.bttn.grid(row=1, column=0, sticky=W) 


     def send(self): 
      self.start=time.time() 

      self.lbl2=Label(self, text="Sending...") 
      self.lbl2.grid(row=1, column=1, sticky=W) 

      self.start.sleep(2) 
      self.lbl2.destroy() 


root=Tk() 
root.title("Mail") 
app=Application(root) 
root.mainloop() 

我得到這個錯誤:

TypeError: send() missing 2 required positional arguments: 'interp' and 'cmd' 

我不明白這是什麼錯誤表示。有人能告訴我這是什麼意思嗎?

回答

0

send函數應該有相同的縮進爲create_widgets__init__功能。在您的send函數中,您必須將self.start.sleep(2)替換爲time.sleep(2)time對象沒有任何睡眠功能。

下面是完整的代碼:

import time, random 
from tkinter import * 

class Application(Frame): 
    def __init__(self, master): 
     Frame.__init__(self, master) 
     self.grid() 
     self.create_widgets() 

    def create_widgets(self): 
     self.lbl1=Label(self, text="Write your message:") 
     self.lbl1.grid(row=0, column=0, columnspan=2, sticky=W) 

     self.entry=Entry(self) 
     self.entry.grid(row=0, column=2, columnspan=3, sticky=W) 

     self.bttn=Button(self, text="Send", command=self.send) 
     self.bttn.grid(row=1, column=0, sticky=W) 


    def send(self): 
     # self.start=time.time() 
     # I think you don't need that anymore... 

     self.lbl2=Label(self, text="Sending...") 
     self.lbl2.grid(row=1, column=1, sticky=W) 

     time.sleep(2) 
     self.lbl2.destroy() 


root=Tk() 
root.title("Mail") 
app=Application(root) 
root.mainloop() 

希望這有助於!

+0

另外,由於send是一個被覆蓋的函數,因此它必須與原始簽名具有相同的簽名:send(self,interp,cmd,* args)。 –

+0

@Walter_Ritzel:_「由於send是被覆蓋的函數,它必須與原始簽名具有相同的簽名」_這並不完全正確。它可以是任何它想要的簽名,只要它只用於新簽名即可。 –

3

您的send函數被縮進,因此它被定義在create_widgets之內。因此,Python不會將其識別爲您的Application類的一種方法,因此command=self.send指的是屬於父類Frame類的現有send方法。

取消您的send方法的縮進,使其與所有其他類方法具有相同的縮進級別。

def create_widgets(self): 
     self.lbl1=Label(self, text="Write your message:") 
     #etc 

    def send(self): 
     self.start=time.time() 
     #etc 

另外,保持縮進,因爲它是,但參數列表改爲def send():,移動def send所以會出現上述self.bttn=Button(self, text="Send", command=self.send)線,並刪除不再需要的self秒。

def create_widgets(self): 
     def send(): 
      self.start=time.time() 

      self.lbl2=Label(self, text="Sending...") 
      self.lbl2.grid(row=1, column=1, sticky=W) 

      self.start.sleep(2) 
      self.lbl2.destroy() 

     self.lbl1=Label(self, text="Write your message:") 
     self.lbl1.grid(row=0, column=0, columnspan=2, sticky=W) 

     self.entry=Entry(self) 
     self.entry.grid(row=0, column=2, columnspan=3, sticky=W) 

     self.bttn=Button(self, text="Send", command=send) 
     self.bttn.grid(row=1, column=0, sticky=W) 
+0

它工作,但現在它給了我這個錯誤:文件「C:/ Python34/python 3.4/my_gmail2.pyw」,第27行,發送 self.start.sleep(2)AttributeError:'float'object has沒有屬性'睡眠' –

+1

這是因爲'time.time()'返回一個浮點數,並且浮點數沒有'sleep'方法。你是否試圖訪問'time'模塊的'sleep'方法?試試'time.sleep(2)'來代替。不需要「自我」或任務。 – Kevin

+0

事實上,在Tkinter程序中可能不建議使用'sleep',因爲它暫時停止主循環並導致應用程序完全無響應。如果您想在某段時間後發生事件,請考慮使用'root.after'。 – Kevin

相關問題