Home > Net >  python game with questions gui
python game with questions gui

Time:03-31

so I am making a little question creepy game for school in python. but I have a problem. when you answer a question I don't know how to switch to the next question. I tried when you click a answer it goes to a new window but that will open up whole lot of windows so idk how to do it. MY CODE SO FAR:

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()

CodePudding user response:

Well instead of creating new windows each time, you can also just make changes to the current window, like delete the current widgets and create new ones, or update the existing ones, in order to ask a new question or continue with your game.

To give you an idea, I've called here two functions for each buttons, and since another question is going to be also a "yes-no" type, I will just make changes to the question label pitanje, depending upon the question the user is at (For that, I'm creating a variable questionNo to keep a track of which question the user is at when a button is pressed.)

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()
  • Related