Skip to content Skip to sidebar Skip to footer

Tkinter Confirmation Buttons And Game Gui (splice(?))

Never used Tkinter before, and I'm not quite sure what to do with it or how it works. Windows IDLE Shell. import time from tkinter import * input('Press enter to begin...') print

Solution 1:

The below code shows how you can achieve this and is commented to explain:

from tkinter import *

root = Tk() #declares that the main window belongs to root
frame = Frame(root) #creates a frame inside the main window so that we don't destroy the actual window when we refreshdefcommand1(frame):
    frame.destroy() #destroys the frame and everything in it
    frame = Frame(root) #new frame
    label1 = Label(frame, text="What is your name?") #text label
    entry1 = Entry(frame) #entry widget
    button1 = Button(frame, text="Ok", command=lambda:command2(frame, entry1)) #continue button
    frame.pack() #packs item
    label1.pack() #packs item
    entry1.pack() #packs item
    button1.pack() #packs itemdefcommand2(frame, entry1):
    var = entry1.get() #gets the text entered in the last phase and stores it before the item is destroyed
    frame.destroy() #destroys the frame and everything in it
    frame = Frame(root) #new frame 
    label1 = Label(frame, text="So %s is your name then? Are you sure?" % var) #text label
    button1 = Button(frame, text="Yes", command=lambda:command3(frame, var)) #yes button
    button2 = Button(frame, text="No", command=lambda:command1(frame)) #no button
    frame.pack() #packs item
    label1.pack() #packs item
    button1.pack() #packs item
    button2.pack() #packs itemdefcommand3(frame, var):
    frame.destroy() #destroys the frame and everything in it
    frame = Frame(root) #new frame
    label1 = Label(frame, text="Then %s, let your adventure begin..." % var) #text label
    frame.pack() #packs item
    label1.pack() #packs item

label1 = Label(frame, text="Press below to begin...") #text label
button1 = Button(frame, text="Begin", command=lambda:command1(frame)) #begin button

frame.pack() #packs item
label1.pack() #packs item
button1.pack() #packs item

root.mainloop() #starts event loop

I would still recommend http://effbot.org/tkinterbook/ as a starting point for tkinter.


The below shows how you can align the two buttons next to each other, the code is commented to show where it varies from the original:

from tkinter import *

root = Tk()
frame = Frame(root)

defcommand1(frame):
    frame.destroy()
    frame = Frame(root)
    label1 = Label(frame, text="What is your name?")
    entry1 = Entry(frame)
    button1 = Button(frame, text="Ok", command=lambda:command2(frame, entry1))
    frame.pack()
    label1.pack()
    entry1.pack()
    button1.pack()

defcommand2(frame, entry1):
    var = entry1.get()
    frame.destroy()
    frame = Frame(root)
    frame1 = Frame(frame) #creates lower frame
    label1 = Label(frame, text="So %s is your name then? Are you sure?" % var)
    button1 = Button(frame1, text="Yes", command=lambda:command3(frame, var)) #this button is now in the lower frame
    button2 = Button(frame1, text="No", command=lambda:command1(frame)) #this button is now in the lower frame
    frame.pack()
    frame1.pack(side="bottom") #packs lower frame
    label1.pack()
    button1.pack(side="left") #packs button left
    button2.pack(side="right") #packs button rightdefcommand3(frame, var):
    frame.destroy()
    frame = Frame(root)
    frame1 = Frame(frame)
    label1 = Label(frame, text="Then %s, let your adventure begin..." % var)
    frame.pack()
    label1.pack()

label1 = Label(frame, text="Press below to begin...")
button1 = Button(frame, text="Begin", command=lambda:command1(frame))

frame.pack()
label1.pack()
button1.pack()

root.mainloop()

Post a Comment for "Tkinter Confirmation Buttons And Game Gui (splice(?))"