gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:32:10 26 4
gpt4 key购买 nike

以前从未使用过 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 中,以便进行迷你文本冒险测试,让人们在玩游戏时更容易导航。因此,我希望将需要输入的"is"和“否”响应转换为按钮,这样玩家就不需要触摸键盘来完成。问题是,我不知道如何将所有数据以及按照我的意图工作的按钮一起放入小 TKinter 界面中。

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

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.

最佳答案

下面的代码展示了如何实现这一点,并添加了注释以进行解释:

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

关于python - Tkinter 确认按钮和游戏 GUI(拼接(?)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45569151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com