gpt4 book ai didi

python - 使用 Tkinter 显示条目文本时出现问题

转载 作者:行者123 更新时间:2023-11-30 21:56:36 25 4
gpt4 key购买 nike

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

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

此后,用户必须单击另一个按钮完成才能查看完整引用。要可视化执行情况,请选择“Harvard”,然后选择“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

最佳答案

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

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

Book = Button(second, text="Book", command=Harvard).grid(column=1, row=1)

这并没有按照您的想法进行,因为 grid() 方法始终返回 None,因此这就是分配给 Book 的值。我还没有修复这些问题,但这样做很简单,只需首先创建小部件并将其分配给一个变量,然后在另一行上,全部 variable.grid(...)

最后一条建议:阅读并开始遵循PEP 8 - Style Guide for Python Code 。我已经在某种程度上对您的代码版本进行了此操作,并修复了如下所示的问题。

仅供引用:问题 Best way to structure a tkinter application 已接受的答案描述了一种优秀的面向对象方法来构造和实现基于 Tkinter 的应用程序。

from tkinter import *


def book_harvard():
global author_entry, year_entry, title_entry # ADDED

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():
global author_entry, year_entry, title_entry # ADDED

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()

关于python - 使用 Tkinter 显示条目文本时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55459133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com