使用Tkinter显示输入文本时出现问题

时间:2019-04-01 15:55:30

标签: python python-3.x tkinter

我是python的新手。我正在尝试编写一个程序来引用哈佛和APA风格的参考书,书籍章节和期刊文章。我需要能够从用户中打印条目。

代码显示三个窗口。在第一个窗口中,要求用户选择引用样式(哈佛/ APA),并在单击按钮时打开另一个窗口,该窗口允许用户选择源(书/书的章节/期刊)。然后将打开第三个窗口,允许用户输入所需的信息(作者/年份/标题等)。

此后,用户必须单击另一个完成按钮以查看完整的参考。为了可视化执行,选择哈佛,然后选择Book,因为我只写了那部分代码。

from tkinter import *

def book_harvard():
    reference = author_entry.get()
    reference_1 = year_entry.get()
    reference_2 = title_entry.get()
    string_to_display = reference + reference_1 + reference_2
    print(string_to_display)


def TypeofTextHarvard():
    second = Toplevel(first)
    first.withdraw()
    second.title("Type of Source")
    Source = Label (second, text = "What type of source would you like to reference?").grid(column = 1, row = 0)
    Book = Button (second, text = "Book", command = Harvard).grid(column = 1, row = 1)
    Chapter = Button (second, text = "Book Chapter").grid (column = 1, row = 2)
    Journal = Button (second, text = "Journal Article").grid (column = 1, row = 3)

first = Tk()
first.title("Referencing Style")
Reference = Label (first, text = "Which referencing style would you like to use?").grid(column = 1, row = 0)
Harvard = Button (first, text = "Harvard Style", command = TypeofTextHarvard).grid (column = 1, row = 1)
APA = Button (first, text = "APA Style").grid (column = 1, row = 2)


def Harvard():
    third = Toplevel()
    third.title("book")
    author = StringVar()
    year = StringVar()
    title = StringVar()

    author_label = Label(third, text = "Author")
    author_entry = Entry(third)
    year_label = Label(third, text = "Year")
    year_entry = Entry(third)
    title_label = Label(third, text = "Title")
    title_entry = Entry(third)
    button_1 = Button(third, text = "Done", command = book_harvard)
    author_label_2 = Label(third, textvariable = author)
    year_label_2 = Label(third, textvariable = year)
    title_label_2 = Label(third, textvariable = title)

    author_label.grid (row = 0, column = 0)
    author_entry.grid (row = 0, column = 1)
    year_label.grid (row = 1, column = 0)
    year_entry.grid (row = 1, column = 1)
    title_label.grid (row = 2, column = 0)
    title_entry.grid (row = 2, column = 1)
    button_1.grid (row = 3, column = 0)
    author_label_2.grid (row = 4, column = 1)
    year_label_2.grid (row = 4, column = 2)
    title_label_2.grid (row = 4, column = 3)


first.mainloop()

但是我遇到以下错误:

reference = author_entry.get()
NameError: name 'author_entry' is not defined

1 个答案:

答案 0 :(得分:3)

NameError的原因是因为author_entryHarvard()函数的局部变量,因此无法在其外部引用,例如在单独的{{1 }}函数。最简单(尽管不是最好)的解决方案是使其成为全局变量。其他book_harvard()小部件可能有相同的问题,因此在下面的代码中,我已将所有Entry声明为它们。

我还注意到您可能会遇到的另一个潜在问题,即将global方法的返回值分配给变量。例如:

grid()

这没有按照您的想法做,因为Book = Button(second, text="Book", command=Harvard).grid(column=1, row=1) 方法始终返回grid(),因此这就是分配给None的值。我尚未修复这些问题,但是这样做很容易,只需先创建小部件并将其分配给变量,然后再在另一行上将所有Book分配给我们即可。

最后一条建议:阅读并开始遵循PEP 8 - Style Guide for Python Code。我已在一定程度上对代码版本进行了修正,如下所示。

仅供参考:对于问题Best way to structure a tkinter application,公认的答案描述了一种构造和实现基于Tkinter的应用程序的出色的面向对象方法。

variable.grid(...)