Python Tkinter:类型错误:'Frame'对象不可调用

1 投票
1 回答
2265 浏览
提问于 2025-04-18 00:39

我创建了一个图形界面程序,里面有几个页面可以切换,最开始我用方法来实现这个功能,一切都运行得很好。但是一旦我引入了一个类,就出现了这个错误:

TypeError: 'Frame' object is not callable

我使用的代码如下:

import sys
from tkinter import *
from tkinter import ttk

class teachingContent(Tk):

    def __init__(self):
        super(teachingContent, self).__init__()

        self.first_title_frame = ttk.Frame()
        self.first_frame = ttk.Frame()

    def nextButtonPressed(self):
        pass

    def prevButtonPressed(self):
        pass

    def formSize(self):
        self.geometry("650x450+200+200") # Sets the size of the gui
        self.title("Python Tutor")

        self.nbutton = Button(text = "Next", command = self.nextButtonPressed).place(x=561,y=418)

        self.pbutton = Button(text = "Previous", command = self.prevButtonPressed).place(x=0,y=418)

        title_height = 40

        self.first_title_frame(self, height=title_height, bg = 'black')
        self.first_title_frame['borderwidth'] = 2
        self.first_title_frame.grid(column=0, row=0, padx=35, pady=5, sticky=(W, N, E))


        self.first_frame(self, bg = 'DarkSlateGray4')
        self.first_frame['borderwidth'] = 2
        self.first_frame['relief'] = 'sunken'
        self.first_frame.grid(column=0, row=0, padx=33, pady=50, sticky=(W, N, E))

        self.label = Label(self.first_title_frame, text = "Welcome to the Tutor").pack()

        self.label = Label(self.first_frame, text = "Welcome to the Python Tutor. In this program you will learn to tools and techniques need to use Python.")
        self.label.grid(column=0, row=0, ipadx=85,pady=11, padx=11, sticky=(N))

tc = teachingContent()
tc.formSize()

我在这行代码中做了一个修改,添加了.configure,变成了:

self.first_title_frame.configure(self, height=title_height, bg = 'black')

但是这样做后,我得到了以下的错误追踪信息:

Traceback (most recent call last):
  File "C:\Users\Tete De Bite\Dropbox\Year3\FinalYearProject\pythonTutoringSystem\debug.py", line 47, in <module>
   tc.formSize()
  File "C:\Users\Tete De Bite\Dropbox\Year3\FinalYearProject\pythonTutoringSystem\debug.py", line 31, in formSize
    self.first_title_frame.configure(self, height=title_height, bg = 'black')
  File "C:\Python33\lib\tkinter\__init__.py", line 1263, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Python33\lib\tkinter\__init__.py", line 1254, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-pady"

有没有人能帮我想想,怎样才能纠正我在编程中犯的错误呢?

1 个回答

3
self.first_title_frame.configure(self, height=title_height, bg = 'black')

在这里,你不需要把 self 作为参数传递。

self.first_title_frame.configure(height=title_height, bg = 'black')

无论如何,ttk.Frame 似乎不允许你直接设置它们的背景颜色。bg这里的“标准选项”或“特定于小部件的选项”中没有列出。试试使用 style 参数,具体可以参考这篇文章

撰写回答