Tkinter Toplevel不工作

时间:2017-10-26 15:15:36

标签: python python-3.x tkinter tkinter-entry

我正在尝试构建一个tkinter程序,它将从主窗口的文本字段中获取搜索字符串,然后使用搜索字符串的结果创建一个子窗口。由于我是新的tkinter,我只是想创建子窗口并将搜索字符串传递给该窗口。运行我的程序时收到错误:

AttributeError: '_tkinter.tkapp' object has no attribute 'Toplevel'

关于专栏:

line 23, in search

是:

results_window = self.parent.Toplevel(self)

以下是代码:

import tkinter as tk

#App Constants
APP_TITLE = 'TKINTER_TEST'
APP_ICON = '\icon\path.ico'

#Geo Constants
MAIN_WIDTH = 600
MAIN_HEIGHT = 300
MAIN_X = '-0'
MAIN_Y = '+0'


class MainApp():
    def __init__(self, parent):
        self.parent = parent

    def get_search(self):
        pass

    def search(self, search_string):
        results_window = self.parent.Toplevel(self)


    def build_widgets(self):
        search_string = tk.StringVar()
        text = tk.Entry(root, textvariable = search_string).pack()
        search_cmd = tk.Button(root, text="Search", command=self.search(search_string.get())).pack()


#MAIN
root = tk.Tk()

#root.geometry('500x300-0+0')
root.geometry('{}x{}{}{}'.format(MAIN_WIDTH, MAIN_HEIGHT, MAIN_X, MAIN_Y))
root.title(APP_TITLE)

main = MainApp(root)
main.build_widgets()

root.mainloop()

我接近这个错误的方式吗?

1 个答案:

答案 0 :(得分:0)

Toplevel是一个小工具,例如Frame

所以第23行必须是: self.result_window = tk.Toplevel(self.parent)