Tkinter确认按钮和游戏GUI(拼接(?)) [英] Tkinter Confirmation buttons and game GUI (splice(?))

查看:112
本文介绍了Tkinter确认按钮和游戏GUI(拼接(?))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前从未使用过Tkinter,而且我不太确定该怎么做或如何工作。 Windows IDLE Shell。

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

如果可能的话,我想将游戏放到用Tkinter制作的小型GUI中,以进行迷你文字冒险测试。当人们玩游戏时,导航变得更加容易。因此,我希望将需要输入的是和否响应都输入按钮,这样播放器就不需要触摸键盘即可完成操作。问题是,我不知道如何将所有数据以及按钮按我的意愿放入小TKinter界面。

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.

推荐答案

下面的代码显示了如何实现此目标,并通过注释进行了解释:

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

我仍然建议 http://effbot.org/tkinterbook/ 作为开始

下面显示了如何将两个按钮彼此对齐,代码被注释为显示与原始版本不同的地方:

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

这篇关于Tkinter确认按钮和游戏GUI(拼接(?))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆