python 有问题的游戏 gui
python game with questions gui
所以我正在 python 为学校制作一个令人毛骨悚然的小问题游戏。
但我有一个问题。当你回答一个问题时我不知道如何切换到下一个问题。我试过当你点击一个答案时它会转到一个新的 window 但这会打开很多 windows 所以我不知道该怎么做。
到目前为止我的代码:
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()
不用每次都创建新的 windows,您也可以只对当前的 window 进行更改,例如删除当前的小部件并创建新的小部件,或者更新现有的小部件,在为了提出新问题或继续您的游戏。
为了给你一个想法,我在这里 为每个按钮调用了两个函数 ,因为另一个问题也将是“yes-no”类型,我将仅更改问题标签 pitanje
,取决于用户所处的问题(为此,我m 创建一个变量 questionNo
来跟踪用户在按下按钮时所处的问题。)
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()
所以我正在 python 为学校制作一个令人毛骨悚然的小问题游戏。 但我有一个问题。当你回答一个问题时我不知道如何切换到下一个问题。我试过当你点击一个答案时它会转到一个新的 window 但这会打开很多 windows 所以我不知道该怎么做。 到目前为止我的代码:
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()
不用每次都创建新的 windows,您也可以只对当前的 window 进行更改,例如删除当前的小部件并创建新的小部件,或者更新现有的小部件,在为了提出新问题或继续您的游戏。
为了给你一个想法,我在这里 为每个按钮调用了两个函数 ,因为另一个问题也将是“yes-no”类型,我将仅更改问题标签 pitanje
,取决于用户所处的问题(为此,我m 创建一个变量 questionNo
来跟踪用户在按下按钮时所处的问题。)
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()