0

我正在尝试通过按钮命令创建一个新的 TopLevel,但是,我遇到了一些困难。使用以下代码:

 initial_state_button = Button(current_state, text=current_state_text, command = partial(initial_state_display, aatsplusv))
 initial_state_button.pack(side = TOP)

....

def initial_state_display(dictionary):

    top = Toplevel()
    top.title = "About this State:"

    count = 0
    for key, value in dictionary["initial states"].iteritems():
        proposition = Message(top, text = key)
        proposition.grid(row=count, column=0, padx=5, pady=5)
        colon = Message(top, text = " : ")
        colon.grid(row=count, column=1, padx=5, pady=5)
        boolean = Message(top, text = str(value))
        colon.grid(row=count, column=2, padx=5, pady=5)
        count += 1

我收到以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__ return self.func(*args)
File "Assignmentest.py", line 400, in initial_state_display top = Toplevel()
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2136, in __init__ self.title(root.title())
TypeError: 'str' object is not callable

我不知道出了什么问题。top = Toplevel 完全按照 effbot 示例所示的方式编写。Python 2.7 中不提供顶层吗?如果是这样,是否有 2.7 等价物?

4

1 回答 1

1

您声明top的标题行是错误的。你的线

top.title = "About this State:"

应该是这样的:

top.title("About this State:")

有关详细信息,请参阅effbot 文档


由于评论而编辑:

我的系统上刚刚安装了 Python3,但是像这个蓝图这样的东西应该可以工作。代码不是很好,但显示了如何处理顶级窗口:

#!/usr/bin/env python3
# coding: utf-8

from tkinter import *


def btn_callback():
    top = Toplevel()
    top.title("Toplevel window")


root = Tk()
root.title('Main window')

b = Button(root, text="Open Toplevel", command=btn_callback)
b.pack()


root.mainloop()

如果您使用的是 Python2,您应该更改tkinterTkinter并且代码应该可以按需要工作。

于 2015-12-06T17:41:41.037 回答