所以我正在用python為學校制作一個小問題令人毛骨悚然的游戲。但我有一個問題。當你回答一個問題時,我不知道如何切換到下一個問題。我試過當你點擊一個答案時,它會進入一個新視窗,但這會打開很多視窗,所以我不知道該怎么做。到目前為止我的代碼:
from tkinter import*
def openNewWin():
openNewWin = Toplevel(win)
openNewWin.attributes('-fullscreen',True)
Label(openNewWin, text = "dali ste sami?", bg="black", fg="white", font= ('Open Sans', 50)).place(x= "570", y= "300")
def openNewWin2():
openNewWin2 = Toplevel(win)
openNewWin2.attributes('-fullscreen',True)
win = Tk()
win.config(bg = "black")
win.attributes('-fullscreen',True)
pitanje = Label(win, text = "dali ste sami?", bg="black", fg="white", font= ('Open Sans', 50)).place(x= "570", y= "300")
odgovor = Button(win, text = "da", bg="black", fg="white", font= ('Open Sans', 50), borderwidth=0, command=openNewWin2)
odgovor.place(x= "560", y= "480")
odgovor2 = Button(win, text = "ne", bg="black", fg="white", font= ('Open Sans', 50), borderwidth=0,command=openNewWin)
odgovor2.place(x= "840", y= "480")
win.mainloop()
uj5u.com熱心網友回復:
除了每次都創建新視窗之外,您還可以對當前視窗進行更改,例如洗掉當前視窗小部件并創建新視窗小部件,或更新現有視窗小部件,以便提出新問題或繼續您的游戲。
為了給您一個想法,我在這里為每個按鈕呼叫了兩個函式,并且由于另一個問題也將是“是-否”型別,因此我將根據用戶的問題對問題標簽進行更改 是在(為此,我正在創建一個變數來跟蹤按下按鈕時用戶所在的問題。)pitanjequestionNo
from tkinter import *
win = Tk()
win.config(bg = "black")
win.attributes('-fullscreen',True)
questionNo=1
def yes():
#whatever you want to happen if answered "yes"
#For example, I will ask another question using that same Label
global questionNo
if questionNo==1 :
pitanje.config(text="Come along, we'll have some fun! Sure that you want to enter?")
elif questionNo==2:
pitanje.config(text="Good decision! Let's begin...")
#and then I can call some other function, or make changes to the widgets of win window only, or can even create a new window to continue further with the game.
else:
return #do nothing, or extend for more number of questions
questionNo =1
def no():
#whatever you want to happen when answered 'no'
#For example, I will ask to leave using that same Label
global questionNo
if questionNo==1 :
pitanje.config(text="You better not be... This is a creepy place to handle alone. Press yes to continue.")
elif questionNo==2:
pitanje.config(text="Oh. So, you are a little coward huh.")
win.after(2000,win.destroy)
else:
return #do nothing right now
questionNo =1
pitanje = Label(win, text = "dali ste sami?", bg="black", fg="white", font= ('Open Sans', 20))
pitanje.place(x= "680", y= "300", anchor=CENTER)
odgovor = Button(win, text = "da", bg="black", fg="white", font= ('Open Sans', 50), borderwidth=0, command=yes)
odgovor.place(x= "550", y= "480", anchor=CENTER)
odgovor2 = Button(win, text = "ne", bg="black", fg="white", font= ('Open Sans', 50), borderwidth=0,command=no)
odgovor2.place(x= "840", y= "480", anchor=CENTER)
win.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/452597.html
