Tkinter在main方法中获取typeerror

时间:2014-08-12 16:39:41

标签: python tkinter

我有一个Tkinter应用程序,它会产生错误, typeerror正好需要3个论点

Traceback (most recent call last):
  File "C:\Python34\aa2.py", line 62, in <module>
    app = simpleapp_tk(None)          # it provides th class name to be connected
TypeError: __init__() takes exactly 3 arguments (2 given)
>>>

我的编码:

import Tkinter
class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent,master):
        Tkinter.Tk.__init__(self,parent,master)
        self.parent = parent
        self.master=master
        self.initialize()                # connects the initialise method
        self.create_widgets()

    def initialize(self):
        self.grid()

        self.entryVariable = Tkinter.StringVar() # makes the entry 
        self.entry1 = Tkinter.Entry(self,textvariable1=self.entryVariable)
        self.entry1.grid(column=0,row=0,sticky='EW')       # connects the grid to the entry
        self.entry1.bind("<Return>", self.OnPressEnter)
        self.entryVariable.set(u"Enter text here.") 
        button = Tkinter.Button(self,text=u"Click me !",                                                               #OUTER CONNECTION(done after inner connections)
                                command=self.OnButtonClick) # connects to the OnbuttonClick method for button click    #
        button.grid(column=1,row=0)          # connects the grid to the button


        self.labelVariable = Tkinter.StringVar()         # makes the label
        label = Tkinter.Label(self,textvariable1=self.labelVariable,
                              anchor="w",fg="white",bg="blue")
        label.grid(column=0,row=1,columnspan=2,sticky='EW') # connects the grid to the label
        self.labelVariable.set(u"Hello !")

        self.grid_columnconfigure(0,weight=1)
        self.resizable(True,False)
        self.update()
        self.geometry(self.geometry())       
        self.entry1.focus_set()
        self.entry1.selection_range(0, Tkinter.END)

    def create_widgets(self):         
        btn1 = Button(self.master, text = "I ")
        btn1.pack()

    def OnButtonClick(self):
        self.labelVariable.set( self.entryVariable.get()+" (You clicked the button)" ) # label variable is connected to the entryvariable #INNER CONNECTION(done within when doing the assignment
        self.entry1.focus_set()
        self.entry1.selection_range(0, Tkinter.END)       

    def OnPressEnter(self,event):
        self.labelVariable.set( self.entryVariable.get()+" (You pressed ENTER)" )     # label variable is connected to the entryvariable
        self.focus_set()
        self.entry1.selection_range(0, Tkinter.END)

if __name__ == "__main__":        # sets the name for the gui that is going to be creitated
    app = simpleapp_tk(None)          # it provides th class name to be connected
    app.title('my application')             # it will set the title
    app.mainloop()

请帮我纠正我的代码!答案将不胜感激!我有一个应用程序,现在我的主要方法必须修改!

2 个答案:

答案 0 :(得分:1)

你的回答就在那里。这一行:

app = simpleapp_tk(None)

...将None传递给定义如下的init例程:

def __init__(self,parent,master):

创建simpleapp_tk()对象时,隐式self与None一起传递;这些是回溯中报告的两个参数,而init例程需要三个参数。您需要修改该硬编码行,以便在创建parent对象时传入mastersimpleapp_tk()

这是一个直接从tkinter reference提取的简单程序的例子:

import Tkinter as tk   

class Application(tk.Frame):          
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)  
        self.grid()                    
        self.createWidgets()

    def createWidgets(self):
        self.quitButton = tk.Button(self, text='Quit',
        command=self.quit)  
        self.quitButton.grid() 

app = Application()
app.master.title('Sample application')
app.mainloop()

在你的init例程仍有问题的情况下编写了那么多代码这一事实告诉我你编写了大量代码而没有进行任何中间调试或检查功能。编程时,通常最好进行小的增量更改,同时确保每个更改在继续下一步之前有效。尝试从这个示例GUI开始,设置基础,然后慢慢添加现有代码中的组件,并确保每个新组件在添加时都有效。

答案 1 :(得分:0)

接近结尾,你有......

app = simpleapp_tk(None)

(只有一个参数)但你的init函数需要另一个参数(父和主)。

所以,应该是......

app = simpleapp_tk(None, None)