相關代碼是這樣的:
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(quiz.Quiz.questions和quiz.Quiz.answers)。
抱歉,如果這段代碼有點草率,但它是我正在嘗試的第一個真正的程式,所以它是一個 WIP 如果您有任何批評或建議,請隨意(盡管這也只是針對我正在努力的計算機科學課程的期末考試)沒原因)。
目前它也只是給出了錯誤代碼:
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
uj5u.com熱心網友回復:
Tk()我想我能夠通過更改StringVars 的創建方式并在創建兩個小部件時添加對它們的參考來獲取當前問題中的代碼(使用兩個s) 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()
uj5u.com熱心網友回復:
您必須使用您創建的 StringVar 物件作為文本變數持有者:
text1 = tk.Entry(menu_root, textvariable=question_response)
和:
text2 = tk.Entry(menu_root, textvariable=answer_response)
并且要使用全域變數,必須在函式外部引入。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/477346.html
