我想制作一款基于文本的冒险游戏。这看起来很简单,我只需制作一棵巨大的 if 语句树,并从用户的每一步中获取输入。然而,后来我决定尝试使用 tkinter 制作一个 GUI。问题是我不知道如何从其中一个按钮获取输入并继续游戏。
我尝试将选择变量实现为 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()
这是一个糟糕的解决方案:你必须退出程序才能继续。
import tkinter as tk
GUI类: def init(自我,标题,路径,故事,btm0,btm1,btm2,btm3): self.root = tk.Tk() self.root.geometry("800x500") self.root.title("迭戈的冒险盛宴!")
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("测试", 'images/pngtest.png', "这就是故事的发展方向", "第一选择", "第二选择", “第三个选择”,“最后一个选择”).run()
如果选择== 0: GUI("错误的选择", 'images/pngtest.png', "这就是故事的发展方向", "第一选择", "第二选择", "第三个选择", "最后一个选择").run() elif 选择 == 1: GUI("再试一次", 'images/pngtest.png', "这就是故事的发展方向", "第一选择", "第二选择", "第三个选择", "最后一个选择").run() elif 选择 == 2: GUI("你不擅长这个", 'images/pngtest.png', "这就是故事的发展方向", "第一选择", "第二选择", "第三个选择", "最后一个选择").run() elif选择== 3: GUI("你明白了", 'images/pngtest.png', "这就是故事的发展方向", "第一选择", "第二选择", “第三个选择”,“最后一个选择”).run()