gpt4 book ai didi

python - 执行 GUI onclick 命令时,将变量值从一个模块返回到另一个模块时出现问题

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

我正在编写一个小型词典程序,用户可以在其中打开另一个窗口来添加新单词和定义,以便可以将其添加到主词典中并稍后进行搜索。

我的问题是,虽然用户可以打开一个新窗口来添加单词及其定义,但单击提交按钮时,主词典不会更新。相反,它会抛出一个错误,UnboundLocalError:赋值前引用了局部变量“word”

我似乎在将新单词/定义变量值传递回主程序时遇到问题。

主程序代码(文件名:Libraryv2.py)

from tkinter import *
import libraryentry



def click():
entered_text = entry.get() #collect text from text entry box
output.delete(0.0,END) #clears text box - start clearing from 0.0 (from line 0) to END (after last character)
try:
definition = my_glossary[entered_text] #this takes the entered text and uses it as a key for the dictionary, the value found is assigned to definition
except:
definition = "No word found in dictionary, try again!"
output.insert(END, definition) #this inserts the contents of variable 'definition' at the beginning (END) - because it was cleared before, END is the at the start

def clickentry():
word = libraryentry.main(word)
definition= libraryentry.main(definition)
my_glossary[word] = definition

window = Tk()

window.title("My Little Dictionary")

#Create the Label
Label(window, text="Enter the word you want defining:").grid(row=0, column=0, sticky=W)

#create entry box
entry=Entry(window, width=20, bg="light green")
entry.grid(row=1, column=0, sticky=W)

#create submit button
Button(window, text="Submit", width=5, command=click).grid(row=2, column=0, sticky=W)

#create second label
Label(window, text="\nDefinition").grid(row=3, column=0, sticky=W)

#create text box
output=Text(window, width=75, height=6, wrap=WORD, background="light green")
output.grid(row=4, column=0, sticky=W)

#create submit button to open enter new definition window
Button(window, text="Enter a New Definition", width=20, command=clickentry).grid(row=5, column=0, sticky=W)


#Create the Dictionary
my_glossary = {
"Algorithm": "Step by step instructions to perform a task",
"Python": "High-Level Programming language used to write light weight programs",
"Flow-Chart": "A way to present an algorthim graphically"
}

window.mainloop()

编辑窗口模块(文件名libraryentry.py):

from tkinter import *

def click2():
word = entry1.get() #collect text from text entry box
definition = entry2.get() #collect text from text entry box
return (word,definition)

def main():
window = Tk()

window.title("Library entry")

#Create the Label
Label(window, text="Enter the word you want to add:").grid(row=0, column=0, sticky=W)

#create entry box
entry1=Entry(window, width=20, bg="light green")
entry1.grid(row=1, column=0, sticky=W)

#create second label
Label(window, text="\nDefinition").grid(row=2, column=0, sticky=W)

#create entry box
entry2=Entry(window, width=50, bg="light green")
entry2.grid(row=3, column=0, sticky=W)

#create submit button
Button(window, text="Submit", width=5, command=click2).grid(row=4, column=0, sticky=W)

return (word, definition)

window.mainloop()

任何解决此问题的帮助都将不胜感激。为什么我的新单词/定义没有正确传递回主程序?

非常感谢

最佳答案

word = libraryentry.main(word)

执行libraryentry.main(word)没有意义,因为word还不存在,而main也不存在无论如何都不接受任何参数。

word = libraryentry.main()

现在您将在 libraryentry 中收到 NameError: name 'word' is not Defined 因为您正在返回 (word,definition)在第二个窗口出现之前,因此用户输入单词并单击“提交”的可能性为 0%。将return移至函数末尾。

window.mainloop()
return (word, definition)

现在,您将在 click2 中收到 NameError: name 'entry1' is not Defined,因为在一个函数中定义的变量在另一个函数中不可见。重构 libraryentry 以使用类,因此 self 的属性可以在方法之间共享。

from tkinter import *

class DefinitionWindow(Toplevel):
def __init__(self, root, click_callback):
Toplevel.__init__(self, root)
self.click_callback = click_callback
self.title("Library entry")

#Create the Label
Label(self, text="Enter the word you want to add:").grid(row=0, column=0, sticky=W)

#create entry box
self.word_entry=Entry(self, width=20, bg="light green")
self.word_entry.grid(row=1, column=0, sticky=W)

#create second label
Label(self, text="\nDefinition").grid(row=2, column=0, sticky=W)

#create entry box
self.definition_entry = Entry(self, width=50, bg="light green")
self.definition_entry.grid(row=3, column=0, sticky=W)

#create submit button
Button(self, text="Submit", width=5, command=self.clicked).grid(row=4, column=0, sticky=W)

def clicked(self):
self.click_callback(self.word_entry.get(), self.definition_entry.get())
self.destroy()

除了 OO 结构之外,请注意没有返回单词和定义的 main。这是由于 Tkinter 的异步特性 - 如果 main 等待定义窗口关闭,则 window.mainloop 无法继续执行,并且一切都会挂起。因此有必要注册一个回调,一旦用户做出选择,该回调就会在将来的某个时间执行。

def clickentry():
def definition_submitted(word, definition):
my_glossary[word] = definition

definition_window = libraryentry.DefinitionWindow(window, definition_submitted)

结果:

Before entering a new definition

Entering a new definition

After entering a new definition

关于python - 执行 GUI onclick 命令时,将变量值从一个模块返回到另一个模块时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23740731/

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