2016-07-22 93 views
-1

我有一個功能,設計運行像root.title(winTitle)。這裏是我的代碼:Python類型錯誤:函數需要1個位置參數,但有2個被給出

from tkinter import * 
class UIWindow(): 
    def __init__(self): 
     Tk() 
    def setWindowTitle(winTitle): 
     self.title(winTitle) 

但是當我運行它,它給人的錯誤:

TypeError: setWindowTitle() takes one positional argument but two was given 

我該如何解決這個問題?

+1

你在方法中缺少'self',因此它會是'def setWindowTitle(self,winTitle)' – kazbeel

+0

請不要改變問題以使代碼正常工作。當你這樣做時,這個問題不再適用。如果您認爲這個問題對未來的讀者會有所幫助,請在答案部分提供答案。如果你不這樣做,請刪除問題。 – zondo

+0

@WozzyCoder我加了自我,但它似乎沒有工作 –

回答

1
from Tkinter import * 
class UIWindow(): 
    def __init__(self, *arg, **kwarg): 
     self.root=Tk(*arg, **kwarg) 
    def setWindowTitle(self, winTitle): 
     self.root.title(winTitle) 

x = UIWindow() 
x.setWindowTitle("This is the Test Title.") 
x.root.mainloop() 

你錯過了自我。這是顯示標題窗口的小例子。

+0

它仍然不起作用 –

+0

更新後的代碼,給你一個示例代碼。這表明要設置標題。查詢總是歡迎:) –

+0

它的工作原理!非常感謝。但爲什麼會發生這種情況。能給我看看麼 ? –

相關問題