提问人:Grayson Chidester 提问时间:8/19/2023 最后编辑:Grayson Chidester 更新时间:8/22/2023 访问量:30
TKINTER,使用按钮进行文字冒险游戏
tkinter, using buttons to progress a text adventure game
问:
我想做一个基于文本的冒险游戏。这似乎很容易,我只是制作了一个巨大的if语句树,并在每一步都从用户那里获得输入。但是,后来我决定尝试使用 Tkinter 制作一个 GUI。问题是我不知道如何从其中一个按钮获取输入并推进游戏。
我尝试将选择变量实现为 ch。按钮 触发更改 ch 的方法。我希望我能检测是否进行了输入并获取 ch 变量。但是,尽管位于主循环中,但 if 语句不会触发。
以下是完整代码:
import tkinter as tk
root = tk.Tk()
#self.root.iconbitmap('')
root.geometry("800x500")
root.title("Diego's Adventure Extravaganza!")
class GUI:
def __init__(self,master):
#the charecter will be able to make choices
self.ch =5
self.index=0
#this will display the chapter the charecter is on
self.title = tk.Label(master, text="Text for titile", font=('Arial',18))
self.title.pack()
#relivent story details
self.storyframe = tk.Frame(master)
self.storyframe.columnconfigure(0, weight=1)
self.storyframe.columnconfigure(1, weight=1)
self.photo = tk.PhotoImage(file='images/pngtest.png')
self.image = tk.Label(self.storyframe, image=self.photo, width= 300, height = 150)
self.image.grid(row=0,column=0,sticky=tk.W+tk.E)
self.story= tk.Label(self.storyframe, text="this is where the story goes")
self.story.grid(row=0,column=1,sticky=tk.W+tk.E)
self.storyframe.pack(fill='x')
self.buttonframe=tk.Frame(master)
self.buttonframe.columnconfigure(0,weight=1)
self.buttonframe.columnconfigure(1,weight=1)
#the choices the chatecter can make at the moment
self.btmZ= tk.Button(self.buttonframe,text="The zero's button", command= self.clickZ)
self.btmZ.grid(row=0, column=0, sticky=tk.W+tk.E)
self.btmO= tk.Button(self.buttonframe,text="the first button", command= self.clickO)
self.btmO.grid(row=0, column=1, sticky=tk.W+tk.E)
self.btmT= tk.Button(self.buttonframe,text="the third button", command= self.clickT)
self.btmT.grid(row=1, column=0, sticky=tk.W+tk.E)
self.btmTh= tk.Button(self.buttonframe,text="the fourth button",command= self.clickTh)
self.btmTh.grid(row=1, column=1, sticky=tk.W+tk.E)
self.buttonframe.pack(fill='x')
def clickZ(self):
self.ch=0
def clickO(self):
self.ch=1
def clickT(self):
self.ch=2
def clickTh(self):
self.ch=3
def cTitle(self,ctext):
self.title.config(text=ctext)
def cImage(self,cpath):
self.photo.config(file=cpath)
def cStory(self,ctext):
self.story.config(text=ctext)
def cBtmText(self,ctext,num):
if num==0:
self.btmZ.config(text=ctext)
elif num == 1:
self.btmO.config(text=ctext)
elif num == 2:
self.btmT.config(text=ctext)
else:
self.btmTh.config(text=ctext)
win = GUI(root)
#The main program
#trying to test the buttons, I want a branching story and for the buttons to do many things
if win.ch ==0:
win.cTitle("Chapter 1")
elif win.ch == 1:
win.cStory("The story begins")
elif win.ch == 2:
win.cbtmText("This is the best button", 2)
else:
win.ch =5
root.mainloop()
答:
0赞
Grayson Chidester
8/19/2023
#1
这是一个糟糕的解决方案:您必须退出程序才能继续前进。
import tkinter as tk
class GUI:
def __init__(self, title, path, story, btm0, btm1, btm2, btm3):
self.root = tk.Tk()
self.root.geometry("800x500")
self.root.title("Diego's Adventure Extravaganza!")
self.title = tk.Label(self.root, text=title, font=('Arial', 18))
self.title.pack()
self.storyframe = tk.Frame(self.root)
self.storyframe.columnconfigure(0, weight=1)
self.storyframe.columnconfigure(1, weight=1)
self.photo = tk.PhotoImage(file=path)
self.image = tk.Label(self.storyframe, image=self.photo, width=300, height=150)
self.image.grid(row=0, column=0, sticky=tk.W + tk.E)
self.story = tk.Label(self.storyframe, text=story)
self.story.grid(row=0, column=1, sticky=tk.W + tk.E)
self.storyframe.pack(fill='x')
self.buttonframe = tk.Frame(self.root)
self.buttonframe.columnconfigure(0, weight=1)
self.buttonframe.columnconfigure(1, weight=1)
self.btmZ = tk.Button(self.buttonframe, text=btm0, command=lambda: self.click(0))
self.btmZ.grid(row=0, column=0, sticky=tk.W + tk.E)
self.btmO = tk.Button(self.buttonframe, text=btm1, command=lambda: self.click(1))
self.btmO.grid(row=0, column=1, sticky=tk.W + tk.E)
self.btmT = tk.Button(self.buttonframe, text=btm2, command=lambda: self.click(2))
self.btmT.grid(row=1, column=0, sticky=tk.W + tk.E)
self.btmTh = tk.Button(self.buttonframe, text=btm3, command=lambda: self.click(3))
self.btmTh.grid(row=1, column=1, sticky=tk.W + tk.E)
self.buttonframe.pack(fill='x')
def click(self, choice):
self.choice = choice
self.root.quit()
def run(self):
self.root.mainloop()
return self.choice
choice = GUI("Testing", 'images/pngtest.png', "this is where the story goes", "First choice", "Second choice",
"third choice", "last choice").run()
if choice == 0:
GUI("Wrong choice", 'images/pngtest.png', "this is where the story goes", "First choice", "Second choice",
"third choice", "last choice").run()
elif choice == 1:
GUI("Try again", 'images/pngtest.png', "this is where the story goes", "First choice", "Second choice",
"third choice", "last choice").run()
elif choice == 2:
GUI("You are bad at this", 'images/pngtest.png', "this is where the story goes", "First choice", "Second choice",
"third choice", "last choice").run()
elif choice == 3:
GUI("you got it", 'images/pngtest.png', "this is where the story goes", "First choice", "Second choice",
"third choice", "last choice").run()
0赞
Grayson Chidester
8/22/2023
#2
根据 TheTechRobo 的评论。
import tkinter as tk
root = tk.Tk()
class GUI:
def __init__(self,master):
self.ch =5
self.index=0
master.geometry("800x500")
master.title("Diego's Adventure Extravaganza!")
self.title = tk.Label(master, text="Text for titile", font=('Arial',18))
self.title.pack()
self.storyframe = tk.Frame(master)
self.storyframe.columnconfigure(0, weight=1)
self.storyframe.columnconfigure(1, weight=1)
self.photo = tk.PhotoImage(file='images/pngtest.png')
self.image = tk.Label(self.storyframe, image=self.photo, width= 300, height = 150)
self.image.grid(row=0,column=0,sticky=tk.W+tk.E)
self.story= tk.Label(self.storyframe, text="this is where the story goes")
self.story.grid(row=0,column=1,sticky=tk.W+tk.E)
self.storyframe.pack(fill='x')
self.buttonframe=tk.Frame(master)
self.buttonframe.columnconfigure(0,weight=1)
self.buttonframe.columnconfigure(1,weight=1)
#the choices the chatecter can make at the moment
self.btmZ= tk.Button(self.buttonframe,text="The zero's button", command= lambda:self.click(0))
self.btmZ.grid(row=0, column=0, sticky=tk.W+tk.E)
self.btmO= tk.Button(self.buttonframe,text="the first button", command= lambda: self.click(1))
self.btmO.grid(row=0, column=1, sticky=tk.W+tk.E)
self.btmT= tk.Button(self.buttonframe,text="the third button", command= lambda: self.click(2))
self.btmT.grid(row=1, column=0, sticky=tk.W+tk.E)
self.btmTh= tk.Button(self.buttonframe,text="the fourth button",command= lambda: self.click(3))
self.btmTh.grid(row=1, column=1, sticky=tk.W+tk.E)
self.buttonframe.pack(fill='x')
def click(self, choice):
if choice == 0:
self.title.config(text="chapter 1")
self.photo.config(file='images/pngtest.png')
self.story.config(text="this is the first line in the story")
self.btmZ.config(text="A button", command =lambda:self.line1(0))
self.btmO.config(text="A button", command=lambda: self.line1(1))
self.btmT.config(text="A button", command=lambda: self.line1(2))
self.btmTh.config(text="A button", command=lambda: self.line1(3))
elif choice == 1:
self.title.config(text="chapter 1")
self.photo.config(file='images/pngtest.png')
self.story.config(text="this is the first line in the story")
self.btmZ.config(text="A button", command=lambda: self.line1(0))
self.btmO.config(text="A button", command=lambda: self.line1(1))
self.btmT.config(text="A button", command=lambda: self.line1(2))
self.btmTh.config(text="A button", command=lambda: self.line1(3))
elif choice ==2:
self.title.config(text="chapter 1")
self.photo.config(file='images/pngtest.png')
self.story.config(text="this is the first line in the story")
self.btmZ.config(text="A button", command=lambda: self.line1(0))
self.btmO.config(text="A button", command=lambda: self.line1(1))
self.btmT.config(text="A button", command=lambda: self.line1(2))
self.btmTh.config(text="A button", command=lambda: self.line1(3))
else:
self.title.config(text="chapter 1")
self.photo.config(file='images/pngtest.png')
self.story.config(text="this is the first line in the story")
self.btmZ.config(text="A button", command=lambda: self.line1(0))
self.btmO.config(text="A button", command=lambda: self.line1(1))
self.btmT.config(text="A button", command=lambda: self.line1(2))
self.btmTh.config(text="A button", command=lambda: self.line1(3))
def line1(self, choice):
if choice == 0:
self.title.config(text="chapter 1")
self.photo.config(file='images/pngtest.png')
self.story.config(text="this is the first line in the story")
self.btmZ.config(text="A button", command =lambda:self.line1(0))
self.btmO.config(text="A button", command=lambda: self.line1(1))
self.btmT.config(text="A button", command=lambda: self.line1(2))
self.btmTh.config(text="A button", command=lambda: self.line1(3))
elif choice == 1:
self.title.config(text="chapter 1")
self.photo.config(file='images/pngtest.png')
self.story.config(text="this is the first line in the story")
self.btmZ.config(text="A button", command=lambda: self.line1(0))
self.btmO.config(text="A button", command=lambda: self.line1(1))
self.btmT.config(text="A button", command=lambda: self.line1(2))
self.btmTh.config(text="A button", command=lambda: self.line1(3))
elif choice ==2:
self.title.config(text="chapter 1")
self.photo.config(file='images/pngtest.png')
self.story.config(text="this is the first line in the story")
self.btmZ.config(text="A button", command=lambda: self.line1(0))
self.btmO.config(text="A button", command=lambda: self.line1(1))
self.btmT.config(text="A button", command=lambda: self.line1(2))
self.btmTh.config(text="A button", command=lambda: self.line1(3))
else:
self.title.config(text="chapter 1")
self.photo.config(file='images/pngtest.png')
self.story.config(text="this is the first line in the story")
self.btmZ.config(text="A button", command=lambda: self.line1(0))
self.btmO.config(text="A button", command=lambda: self.line1(1))
self.btmT.config(text="A button", command=lambda: self.line1(2))
self.btmTh.config(text="A button", command=lambda: self.line1(3))
win = GUI(root)
root.mainloop()
评论
0赞
Grayson Chidester
8/22/2023
如果您无法返回按钮输入,那么拥有 GUI 类的意义何在?
评论
root.mainloop()
cTitle
cStory
cbtmText
clickZ
clickO