Tkinter确认按钮和游戏GUI(拼接(?))

时间:2017-08-08 13:04:24

标签: python user-interface templates tkinter alignment

之前从未使用过Tkinter,我不太确定该怎么做或它是如何工作的。 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,只是为了让迷你文本冒险测试我在人们玩它时更容易导航,因此我希望转动所需的"是& #34;和"不"需要输入按钮的响应,因此播放器不需要触摸键盘即可完成。问题是我不知道如何将所有数据放入小tkiner界面以及按照我的意图工作的按钮。

我可以在非常基本的层面(可能甚至不正确)创建保存按钮和按钮本身的根,但我不知道如何将参数和变量链接到按钮,也不知道如何将文本放入创建的控制台,我的所有尝试都只是以循环结束或控制台根本无法打开。

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

我感谢任何提前帮助的人,即使是模板也可以提供很好的帮助,因为我可以自定义和修改,直到它符合我的需要,但如果有人愿意为我做一个简单的模板基于下面的图片我将不胜感激,因为我希望它遵循类似的简单流程。对不起,如果图像不够清晰。如果可能的话,可能还有一些关于对齐所述按钮和文本的建议。

Possible template, grateful for.

1 个答案:

答案 0 :(得分:2)

以下代码显示了如何实现这一目标并进行了评论以解释:

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/作为tkinter的起点。

下面显示了如何将两个按钮对齐,代码被注释以显示它与原始按钮的不同之处:

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