Tkinter Confirmation buttons and game GUI (splice(?))

最后都变了- 提交于 2020-01-05 04:12:06

问题


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 ("Welcome Traveller!") 
time.sleep(1)
def name_identify():
    print () 
    global name_input
    name_input = input ("What is your name? ").lower() 
    name_input = name_input.title()
    time.sleep(0.75)
    def name_confirm(): 
        print ()
        print ("So %s is your name then?" % name_input)
        time.sleep(1.5)
        print ()
        confirmation = input ("Are you sure? Yes or No? ").lower()
        if confirmation == "Yes" or confirmation == "yes" or confirmation == "aye" or confirmation == "yay":
            print("")
            print ("Then %s, let your adventure begin..." % name_input)
        elif confirmation == "no" or confirmation == "No" or confirmation == "nay":
            name_identify()
        else:
            print ("Please answer with either yes or no young traveller.")
            time.sleep(2)
            name_confirm() 
    name_confirm()        
name_identify()

If possible I would like to put the game into a small GUI made with Tkinter just to make the mini text adventure tests I'm making easier to navigate when people play it. As such, I wish to turn the required "yes" and "no" responses needing to be typed into buttons, so the player doesn't need to touch the keyboard to finish. Problem is, I have no idea how to get all the data into the little TKinter interface along with the buttons working how I intend.

I can create the root which holds the buttons themselves, on a very basic level(likely not even correctly), but I do not know how to link parameters and variables to the buttons, nor how to place the text into the created console. All my attempts have simply ended with loops or the console simply not opening.

from tkinter import *

def main():
    root = Tk()
    root.title("Tkinter Test")
    root.minsize(width=200, height=120)
    root.maxsize(width=400, height=240)

    button = Button(root, text="This is a button!", width=20, height=5)
    button.pack()

    root.mainloop()
if __name__ == '__main__':
    main()

I thank whoever helps in advance. Even a template to work off would be great help, as I can just customise and modify until it suits my needs, but if someone would be so kind as to make a simple template for me based on the below image I would be grateful, as I would like it to follow a simple enough flow similar to that. And sorry if the image isn't clear enough. And maybe some advice on aligning said buttons and text, if it's possible for you.


回答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 refresh

def command1(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 item

def command2(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 item

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

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

def command2(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 right

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


来源:https://stackoverflow.com/questions/45569151/tkinter-confirmation-buttons-and-game-gui-splice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!