AttributeError背后的原因是什么:' int'对象没有属性' items'?

时间:2018-01-25 21:18:31

标签: python tkinter

错误追踪:

_cnfmerge: fallback due to: 'int' object is not iterable
Traceback (most recent call last):
  File "/Users/.../Untitled.py", line 25, in <module>
    app = AddFrame(master=root, title=titleres, width=400)
  File "/Users/.../Untitled.py", line 8, in __init__
    Frame.__init__(self, master, width)   #height error gives need only 
3 arguments , 4 given find out    Frame.__init__(self, master, width, height, bg)TypeError: __init__() takes at most 3 arguments (5 given)
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 2561, in __init__
            cnf = _cnfmerge((cnf, kw))
 "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 119, in _cnfmerge
        for k, v in c.items():
    AttributeError: 'int' object has no attribute 'items'

我的代码:

import Tkinter as tk
from Tkinter import *
import datetime
class AddFrame(tk.Frame):
     def __init__(self, master=None, title = "default_title", width=400, height=400, bg='blue') :
         tk.Frame.__init__(self, master, width) 

         self.grid()
         self.master.title(title)

root = Tk()
root.title('Skeleton Test 1')
root.geometry('1920x1080')



ffcu0 = Frame(root, bg='blue', width=1080, height=700)
ffcu1 = Frame(root, bg='red', width=1080, height = 480)
ffcu2 = Frame(root, bg='green', width=270, height = 700)
ffcu3 = Frame(root, bg='blue', width=270, height = 480)
titleres='research'
app = AddFrame(master=root, title=titleres, width=400)


ffcu0.grid(row=0, column=0)
ffcu1.grid(row=2, column=0)
ffcu2.grid(row=0, column=1)
ffcu3.grid(row=2, column=1)

app.mainloop()

1 个答案:

答案 0 :(得分:4)

width是仅限关键字的参数。您必须以width=width的形式提供它。

class AddFrame(tk.Frame):
     def __init__(self, master=None, title = "default_title", width=400, height=400, bg='blue') :
         tk.Frame.__init__(self, master, width=width) 

tkinter小部件的所有初始化参数都只是关键字,selfmaster除外。