如何在另一個函式中再次使用一個函式中的 entry.get ?
我想用answer1.get這是def question1()再次question2()...
所以我想要這個:
如果用戶對第一個問題給出正確答案,那么他們可以看到第二個問題,否則他們應該對第一個問題給出正確答案
那可能嗎 ?
from tkinter import *
screen = Tk()
screen.geometry("1540x821")
screen.title("Exam")
screen.state("zoomed")
screen.resizable(0, 0)
def question1():
q1 = Label(screen, text="This is the Question 1 : ")
q1.pack()
a1 = StringVar()
answer1 = Entry(screen, textvariable=a1)
answer1.pack()
def check():
if "shopping" in answer1.get():
correct = Label(screen, text="correct answer")
correct.place(relx=0.5, rely=0.1)
else:
wrong = Label(screen, text="wrong answer")
wrong.place(relx=0.5, rely=0.1)
buttoncheck = Button(screen, text="Check", command=check)
buttoncheck.place(relx=0.5, rely=0.4)
def question2():
if "shopping" in answer.get():
q2 = Label(screen, text="This is the Question 2 : ")
q2.pack()
a2 = StringVar()
answer2 = Entry(screen, textvariable=a2)
answer2.pack()
else:
previous = Label(screen, text="go back to previous question")
previous.pack()
button1 = Button(screen, text="Start", command=question1)
button1.place(relx=0.5, rely=0.5)
button2 = Button(screen, text="Next", command=question2)
button2.place(relx=0.5, rely=0.6)
screen.mainloop()
uj5u.com熱心網友回復:
您不能在另一個函式中使用在一個函式內宣告的函式。只需移至宣告check()之外question1()。
作為旁注,您很少想宣告嵌套函式,因為您希望使函式保持合理的簡短。
uj5u.com熱心網友回復:
首先,將check()函式移出question1(). 這與@Amongalen 提到的原因相同。
然后你可以把這段代碼放在check()函式中:
if "shopping" in answer1.get():
correct = Label(screen, text="correct answer")
correct.place(relx=0.5, rely=0.1)
question2()
else:
wrong = Label(screen, text="wrong answer")
wrong.place(relx=0.5, rely=0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/376260.html
標籤:Python 用户界面 特金特 tkinter-入口
上一篇:從vimeo播放器中洗掉ui元素
