StringVar.get() 似乎并没有完全正常工作(Tkinter)

StringVar.get() doesn't seem to be entirely working (Tkinter)

相关代码是这样的:

import tkinter as tk
from tkinter import ttk
import quiz

global menu_root

def get_question():
    question_input = question_response.get()
    answer_input = answer_response.get()
    quiz.add_question(question_input, answer_input)

def question_menu():
    menu_root = tk.Tk()
    global question_response
    global answer_response
    menu_root.geometry("250x250")
    menu_root.resizable(width=False, height=False)
    menu_root.title("Add A Question!")
    question_response = tk.StringVar()
    answer_response = tk.StringVar()
    tk.Label(menu_root, text="What question would you like to add?",
             font=("TkDefault", 10)).place(x=15, y=25)
    text1 = tk.Entry(menu_root,)
    text1.place(x=15, y=45)
    tk.Label(menu_root, text="What's the answer to the question?",
             font=("TkDefault", 10)).place(x=15, y=60)
    text2 = tk.Entry(menu_root)
    text2.place(x=15, y=80)
    tk.Button(menu_root, text="Confirm?", font=("TkDefault", 10),
              command=get_question).place(x=15, y=100)
    menu_root.mainloop()

import tkinter as tk
from tkinter import ttk
import buttons
import quiz

def add_button_gui():
    buttons.question_menu()

def dev():
    print(quiz.Quiz.questions)
    print(quiz.Quiz.answers)

def main_gui():
    main_root = tk.Tk()
    main_root.resizable(width=False, height=False)
    main_root.geometry("500x500")
    main_root.title("Pyquiz")
    main_window = tk.Frame(main_root, borderwidth=5, relief="raised")
    main_label = tk.Label(main_root, text="PyQuiz", 
                          font=("TkDefault", 35)).place(x=165, y=0)
    add_button = tk.Button(main_root, text="Add A Question", font=("TkDefault", 25), 
                           command=add_button_gui).place(x=125, y=115)
    remove_button = tk.Button(main_root, text="Remove Latest Question", 
                            font=("TkDefault", 25)).place(x=70, y=200)
    test_button = tk.Button(main_root, text="Test Your Knowledge", 
                            font=("TkDefault", 25)).place(x=90, y=285)
    save_button = tk.Button(main_root, text="Save Questions", 
                            font=("TkDefault", 25)).place(x=110, y=375)
    dev_button = tk.Button(main_root, text="DEVTEST", font=("TkDefault", 25), 
                           command=dev).place(x=100, y=450)
    main_root.mainloop()

现在,我想要代码做的是打开第二个菜单 (menu_root) 并让用户有 2 个输入槽,当按下按钮时,它会获取输入中的任何字符串并添加它到我在另一个脚本中的列表(quiz.Quiz.questionsquiz.Quiz.answers)。

如果这段代码有点草率,我深表歉意,但这是我正在尝试的第一个真正的程序,所以它是 W.I.P。如果您有任何批评或建议,请随时提出(尽管这也只是针对计算机科学 class final,我无缘无故地努力尝试)。

此外,目前它只给出错误代码:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "C:\Users\heath\PycharmProjects\pythonProject\PyQuiz\buttons.py", line 11, in get_question
    question_input = question_response.get(question_response)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 544, in get
    value = self._tk.globalgetvar(self._name)
AttributeError: 'NoneType' object has no attribute 'globalgetvar'

Process finished with exit code 0

您必须使用您创建的 StringVar 对象作为文本变量持有者:

text1 = tk.Entry(menu_root, textvariable=question_response)

和:

text2 = tk.Entry(menu_root, textvariable=answer_response)

并且要使用全局变量必须在函数外引入。

我想我能够通过更改 StringVars 的创建方式并在两个Entry 个小部件已创建。我用 <-- CHANGED 注释指出了下面代码中的重要更改。注意我禁用了对 quiz 模块的引用,因为你没有在你的问题中提供它的代码。

buttons.py 文件:

import tkinter as tk
from tkinter import ttk
#import quiz

global menu_root

def get_question():
    question_input = question_response.get()
    print(f'{question_input=!r}')
    answer_input = answer_response.get()
    print(f'{answer_input=!r}')
#    quiz.add_question(question_input, answer_input)

def question_menu():
    menu_root = tk.Tk()
    global question_response
    global answer_response
    menu_root.geometry("250x250")
    menu_root.resizable(width=False, height=False)
    menu_root.title("Add A Question!")
    question_response = tk.StringVar(master=menu_root)  # <-- CHANGED
    answer_response = tk.StringVar(master=menu_root)    # <-- CHANGED
    tk.Label(menu_root, text="What question would you like to add?", font=("TkDefault", 10)).place(
        x=15, y=25)
    text1 = tk.Entry(menu_root, textvariable=question_response)  # <-- CHANGED
    text1.place(x=15, y=45)
    tk.Label(menu_root, text="What's the answer to the question?", font=("TkDefault", 10)).place(
        x=15, y=60)
    text2 = tk.Entry(menu_root, textvariable=answer_response)  # <-- CHANGED
    text2.place(x=15, y=80)
    tk.Button(menu_root, text="Confirm?", font=("TkDefault", 10),
              command=get_question).place(x=15, y=100)
    menu_root.mainloop()