Tkinter GUI 帮助
Tkinter GUI Assistance
几周前我寻求帮助,但被告知我的问题或要求太含糊了。
我目前正在为我的 A Level 项目重建 zork,我已经完成了实际的故事情节,但还没有制作 GUI,我也从未制作过 GUI。
我想把它留到最后,让实际的程序运行。
我需要制作这个 GUI,否则我的项目将不会获得评分,请阅读以下内容[注意:我正在使用 TKINTER]:
我需要做的就是有一个输入框,允许用户在游戏中输入命令。
显示游戏文本的文本框,以便他们可以看到故事情节。
我不知道如何完全做到这一点。这只是代码示例,因此您可以了解实际项目。
如有任何帮助,我们将不胜感激!
#from tkinter import *
#root = Tk()
#root.title("Zork Project")
#root.geometry("500x500")
#root.mainloop()
import os
loop = 3
while loop == 3:
print("-----------------------------------------------------------------------------------")
print("------------------Welcome To Alex's Zork Project - Python Edition------------------")
print("-----------------------------------Version 0.0.2-----------------------------------")
print("-----------------------------------------------------------------------------------")
print("---You are standing in an open plain, to the north is an abandoned looking house---")
print("----To the south is a forest, next to you is a broken lockbox screwed to a post----")
print("---------------------------There is a door to the east-----------------------------")
print("-----------------------------------------------------------------------------------")
#First scenario & first input.
first = input("What do you plan to do? ")
if first.lower() == ("open lockbox"):
print("-----------------------------------------------------------------------------------")
print("Upon opening the lockbox it reveals a file stating on the front:")
print("'INGSOC; Ministry of Peace'")
print("Upon opening the file you see 'Mission Objective: Reclaim Lost Records.")
loop = 4
elif first.lower() == ("take lockbox"):
print("-----------------------------------------------------------------------------------")
print("This is not possible, it is screwed on too tightly.")
loop = 4
elif first.lower() == ("n"):
print("You step closer to the house, not much going on there.")
print("You go back to the lockbox...")
loop = 4
elif first.lower() == ("north"):
print("You step closer to the house, not much going on there.")
print("You go back to the lockbox...")
loop = 4
elif first.lower() == ("go north"):
print("You step closer to the house, not much going on there.")
print("You go back to the lockbox...")
loop = 4
elif first.lower() == ("east"):
print("-----------------------------------------------------------------------------------")
print("There is a door that is boarded up, it is not possible to remove them.")
loop = 4
elif first.lower() == ("e"):
print("-----------------------------------------------------------------------------------")
print("There is a door that is boarded up, it is not possible to remove them.")
elif first.lower() == ("go east"):
print("-----------------------------------------------------------------------------------")
print("There is a door that is boarded up, it is not possible to remove them.")
loop = 4
elif first.lower() == ("open door"):
print("-----------------------------------------------------------------------------------")
print("It is not possible to open the door.")
loop = 4
elif first.lower() == ("remove boards"):
print("-----------------------------------------------------------------------------------")
print("The boards are fastened securely, we cannot physically remove them!")
loop = 4
elif first.lower() == ("look at house"):
print("-----------------------------------------------------------------------------------")
print("The house is desolate and abandoned, it may have been used by the resistance as an outpost many moons ago.")
loop = 4
elif first.lower() == ("read file"):
print("-----------------------------------------------------------------------------------")
print("The file says: 'INGSOC; MoP mission to obtain lost mission files from the 'Resistance.'")
print("The file name is labelled 'Op Banner II - MoP reclimation of mission files.'")
loop = 4
elif first.lower() == ("jump"):
print("-----------------------------------------------------------------------------------")
print("How High?")
loop = 4
elif first.lower() == ("south"):
loop = 8
elif first.lower() == ("go south"):
loop = 8
elif first.lower() == ("s"):
loop = 8
else:
print("-----------------------------------------------------------------------------------")
print("Command Unknown. For control help please type 'help'.")
loop = 4
Tkinter 是一个非常易于使用的 GUI 库。从您在问题下的评论来看,您已经尝试但未能理解 Tkinter 的基础知识。您可能想知道,如果您创建主循环,然后在主循环之后编写代码,它将不会在您的 GUI 中使用。主循环需要在你的程序结束时进行。
我们不使用 input
和 print
,而是使用 get()
和 insert
。
get()
用于检索输入字段的值作为字符串。
insert()
用于将值直接写入给定索引处的文本框。
下面是使用现有代码的简单 tkinter GUI。我删除了许多打印行,因为您可以使用三重引号 """
获得相同的结果,因为三重引号是多行引号。这不是最干净的做事方式,但在这里应该可以正常工作。
我在您程序中使用的所有字符串的末尾添加了一个 \n
以使文本框看起来更干净一些,因为它将所有内容移至新行。
我不确定你是否想在每次用户提交命令时保留所有文本,或者你是否想每次都清理面板,所以我继续保留文本。
我们这里不需要 while
循环,因为一切都可以用函数来管理。
我们这里不需要import os
。
我们可以通过创建一个函数来为我们更新文本框,而不是在每个 if/else 语句中使用命令,从而节省一些代码行。
下面的 GUI 很简单,因为它只包含一个文本框和一个输入字段。
我已将 Enter/Return 按钮绑定到输入字段,以便您可以在键入命令后提交命令。下面的代码应该可以提供一个简单的 GUI 所需的一切,它可以满足您的需要。请记住,可能有更好的方式来提供您的回复。可能有一本字典,其中您的命令是键,对文本框的响应是键值,但您可以算出一个。
import tkinter as tk
root = tk.Tk()
root.columnconfigure(0, weight = 1)
root.rowconfigure(0, weight = 1)
root.rowconfigure(1, weight = 0)
#I do not know if you have plans for this loop variable so I left it in the code.
loop = 3
# we need to put the geometry manager grid() after the creation of the widget.
# this will allow us to work with the widget later in the program.
Main_Textbox = tk.Text(root, width = 83, height = 20)
Main_Textbox.grid(row=0, column=0, sticky="nsew")
Command_Line = tk.Entry(root)
Command_Line.grid(row=1, column=0, sticky="ew")
Command_Line.config(background = "black", foreground = "white")
def start_game():
Starting_Text = """
-----------------------------------------------------------------------------------
------------------Welcome To Alex's Zork Project - Python Edition------------------
-----------------------------------Version 0.0.2-----------------------------------
-----------------------------------------------------------------------------------
---You are standing in an open plain, to the north is an abandoned looking house---
----To the south is a forest, next to you is a broken lockbox screwed to a post----
---------------------------There is a door to the east-----------------------------
-----------------------------------------------------------------------------------"""
# the delete() method is used to clear the text box.
Main_Textbox.delete(1.0, "end")
Main_Textbox.insert("end", Starting_Text)
def update_textbox(x):
Main_Textbox.insert("end", x)
# the see() method is used to move the view of the text box down with the new text.
Main_Textbox.see("end")
Command_Line.delete(0, "end")
def process_commands(Event=None):
if Command_Line.get().lower() == ("open lockbox"):
update_textbox("""-----------------------------------------------------------------------------------
Upon opening the lockbox it reveals a file stating on the front:
'INGSOC; Ministry of Peace'
Upon opening the file you see 'Mission Objective: Reclaim Lost Records.\n""")
elif Command_Line.get().lower() == ("take lockbox"):
update_textbox("""-----------------------------------------------------------------------------------
This is not possible, it is screwed on too tightly.\n""")
loop = 4
elif Command_Line.get().lower() == ("n"):
update_textbox("""You step closer to the house, not much going on there.
You go back to the lockbox...\n""")
loop = 4
elif Command_Line.get().lower() == ("north"):
update_textbox("""You step closer to the house, not much going on there.
You go back to the lockbox...\n""")
loop = 4
elif Command_Line.get().lower() == ("go north"):
update_textbox("""You step closer to the house, not much going on there.
You go back to the lockbox...\n""")
loop = 4
elif Command_Line.get().lower() == ("east"):
update_textbox("""-----------------------------------------------------------------------------------
There is a door that is boarded up, it is not possible to remove them.\n""")
loop = 4
elif Command_Line.get().lower() == ("e"):
update_textbox("""-----------------------------------------------------------------------------------
There is a door that is boarded up, it is not possible to remove them.\n""")
loop = 4
elif Command_Line.get().lower() == ("go east"):
update_textbox("""-----------------------------------------------------------------------------------
There is a door that is boarded up, it is not possible to remove them.\n""")
loop = 4
elif Command_Line.get().lower() == ("open door"):
update_textbox("""-----------------------------------------------------------------------------------
It is not possible to open the door.\n""")
loop = 4
elif Command_Line.get().lower() == ("remove boards"):
update_textbox("""-----------------------------------------------------------------------------------
The boards are fastened securely, we cannot physically remove them!\n""")
loop = 4
elif Command_Line.get().lower() == ("look at house"):
update_textbox("""-----------------------------------------------------------------------------------
The house is desolate and abandoned, it may have been used by the resistance as an outpost many moons ago.\n""")
loop = 4
elif Command_Line.get().lower() == ("read file"):
update_textbox("""-----------------------------------------------------------------------------------
The file says: 'INGSOC; MoP mission to obtain lost mission files from the 'Resistance.'
The file name is labelled 'Op Banner II - MoP reclimation of mission files.'""")
loop = 4
elif Command_Line.get().lower() == ("jump"):
update_textbox("""-----------------------------------------------------------------------------------
How High?\n""")
loop = 4
elif Command_Line.get().lower() == ("south"):
loop = 8
elif Command_Line.get().lower() == ("go south"):
loop = 8
elif Command_Line.get().lower() == ("s"):
loop = 8
else:
update_textbox("""-----------------------------------------------------------------------------------
Command Unknown. For control help please type 'help'.\n""")
loop = 4
# this bind() method will allow us to submit our command for processing.
Command_Line.bind("<Return>", process_commands)
start_game()
root.mainloop()
几周前我寻求帮助,但被告知我的问题或要求太含糊了。 我目前正在为我的 A Level 项目重建 zork,我已经完成了实际的故事情节,但还没有制作 GUI,我也从未制作过 GUI。 我想把它留到最后,让实际的程序运行。
我需要制作这个 GUI,否则我的项目将不会获得评分,请阅读以下内容[注意:我正在使用 TKINTER]: 我需要做的就是有一个输入框,允许用户在游戏中输入命令。 显示游戏文本的文本框,以便他们可以看到故事情节。
我不知道如何完全做到这一点。这只是代码示例,因此您可以了解实际项目。 如有任何帮助,我们将不胜感激!
#from tkinter import *
#root = Tk()
#root.title("Zork Project")
#root.geometry("500x500")
#root.mainloop()
import os
loop = 3
while loop == 3:
print("-----------------------------------------------------------------------------------")
print("------------------Welcome To Alex's Zork Project - Python Edition------------------")
print("-----------------------------------Version 0.0.2-----------------------------------")
print("-----------------------------------------------------------------------------------")
print("---You are standing in an open plain, to the north is an abandoned looking house---")
print("----To the south is a forest, next to you is a broken lockbox screwed to a post----")
print("---------------------------There is a door to the east-----------------------------")
print("-----------------------------------------------------------------------------------")
#First scenario & first input.
first = input("What do you plan to do? ")
if first.lower() == ("open lockbox"):
print("-----------------------------------------------------------------------------------")
print("Upon opening the lockbox it reveals a file stating on the front:")
print("'INGSOC; Ministry of Peace'")
print("Upon opening the file you see 'Mission Objective: Reclaim Lost Records.")
loop = 4
elif first.lower() == ("take lockbox"):
print("-----------------------------------------------------------------------------------")
print("This is not possible, it is screwed on too tightly.")
loop = 4
elif first.lower() == ("n"):
print("You step closer to the house, not much going on there.")
print("You go back to the lockbox...")
loop = 4
elif first.lower() == ("north"):
print("You step closer to the house, not much going on there.")
print("You go back to the lockbox...")
loop = 4
elif first.lower() == ("go north"):
print("You step closer to the house, not much going on there.")
print("You go back to the lockbox...")
loop = 4
elif first.lower() == ("east"):
print("-----------------------------------------------------------------------------------")
print("There is a door that is boarded up, it is not possible to remove them.")
loop = 4
elif first.lower() == ("e"):
print("-----------------------------------------------------------------------------------")
print("There is a door that is boarded up, it is not possible to remove them.")
elif first.lower() == ("go east"):
print("-----------------------------------------------------------------------------------")
print("There is a door that is boarded up, it is not possible to remove them.")
loop = 4
elif first.lower() == ("open door"):
print("-----------------------------------------------------------------------------------")
print("It is not possible to open the door.")
loop = 4
elif first.lower() == ("remove boards"):
print("-----------------------------------------------------------------------------------")
print("The boards are fastened securely, we cannot physically remove them!")
loop = 4
elif first.lower() == ("look at house"):
print("-----------------------------------------------------------------------------------")
print("The house is desolate and abandoned, it may have been used by the resistance as an outpost many moons ago.")
loop = 4
elif first.lower() == ("read file"):
print("-----------------------------------------------------------------------------------")
print("The file says: 'INGSOC; MoP mission to obtain lost mission files from the 'Resistance.'")
print("The file name is labelled 'Op Banner II - MoP reclimation of mission files.'")
loop = 4
elif first.lower() == ("jump"):
print("-----------------------------------------------------------------------------------")
print("How High?")
loop = 4
elif first.lower() == ("south"):
loop = 8
elif first.lower() == ("go south"):
loop = 8
elif first.lower() == ("s"):
loop = 8
else:
print("-----------------------------------------------------------------------------------")
print("Command Unknown. For control help please type 'help'.")
loop = 4
Tkinter 是一个非常易于使用的 GUI 库。从您在问题下的评论来看,您已经尝试但未能理解 Tkinter 的基础知识。您可能想知道,如果您创建主循环,然后在主循环之后编写代码,它将不会在您的 GUI 中使用。主循环需要在你的程序结束时进行。
我们不使用 input
和 print
,而是使用 get()
和 insert
。
get()
用于检索输入字段的值作为字符串。
insert()
用于将值直接写入给定索引处的文本框。
下面是使用现有代码的简单 tkinter GUI。我删除了许多打印行,因为您可以使用三重引号 """
获得相同的结果,因为三重引号是多行引号。这不是最干净的做事方式,但在这里应该可以正常工作。
我在您程序中使用的所有字符串的末尾添加了一个 \n
以使文本框看起来更干净一些,因为它将所有内容移至新行。
我不确定你是否想在每次用户提交命令时保留所有文本,或者你是否想每次都清理面板,所以我继续保留文本。
我们这里不需要 while
循环,因为一切都可以用函数来管理。
我们这里不需要import os
。
我们可以通过创建一个函数来为我们更新文本框,而不是在每个 if/else 语句中使用命令,从而节省一些代码行。
下面的 GUI 很简单,因为它只包含一个文本框和一个输入字段。 我已将 Enter/Return 按钮绑定到输入字段,以便您可以在键入命令后提交命令。下面的代码应该可以提供一个简单的 GUI 所需的一切,它可以满足您的需要。请记住,可能有更好的方式来提供您的回复。可能有一本字典,其中您的命令是键,对文本框的响应是键值,但您可以算出一个。
import tkinter as tk
root = tk.Tk()
root.columnconfigure(0, weight = 1)
root.rowconfigure(0, weight = 1)
root.rowconfigure(1, weight = 0)
#I do not know if you have plans for this loop variable so I left it in the code.
loop = 3
# we need to put the geometry manager grid() after the creation of the widget.
# this will allow us to work with the widget later in the program.
Main_Textbox = tk.Text(root, width = 83, height = 20)
Main_Textbox.grid(row=0, column=0, sticky="nsew")
Command_Line = tk.Entry(root)
Command_Line.grid(row=1, column=0, sticky="ew")
Command_Line.config(background = "black", foreground = "white")
def start_game():
Starting_Text = """
-----------------------------------------------------------------------------------
------------------Welcome To Alex's Zork Project - Python Edition------------------
-----------------------------------Version 0.0.2-----------------------------------
-----------------------------------------------------------------------------------
---You are standing in an open plain, to the north is an abandoned looking house---
----To the south is a forest, next to you is a broken lockbox screwed to a post----
---------------------------There is a door to the east-----------------------------
-----------------------------------------------------------------------------------"""
# the delete() method is used to clear the text box.
Main_Textbox.delete(1.0, "end")
Main_Textbox.insert("end", Starting_Text)
def update_textbox(x):
Main_Textbox.insert("end", x)
# the see() method is used to move the view of the text box down with the new text.
Main_Textbox.see("end")
Command_Line.delete(0, "end")
def process_commands(Event=None):
if Command_Line.get().lower() == ("open lockbox"):
update_textbox("""-----------------------------------------------------------------------------------
Upon opening the lockbox it reveals a file stating on the front:
'INGSOC; Ministry of Peace'
Upon opening the file you see 'Mission Objective: Reclaim Lost Records.\n""")
elif Command_Line.get().lower() == ("take lockbox"):
update_textbox("""-----------------------------------------------------------------------------------
This is not possible, it is screwed on too tightly.\n""")
loop = 4
elif Command_Line.get().lower() == ("n"):
update_textbox("""You step closer to the house, not much going on there.
You go back to the lockbox...\n""")
loop = 4
elif Command_Line.get().lower() == ("north"):
update_textbox("""You step closer to the house, not much going on there.
You go back to the lockbox...\n""")
loop = 4
elif Command_Line.get().lower() == ("go north"):
update_textbox("""You step closer to the house, not much going on there.
You go back to the lockbox...\n""")
loop = 4
elif Command_Line.get().lower() == ("east"):
update_textbox("""-----------------------------------------------------------------------------------
There is a door that is boarded up, it is not possible to remove them.\n""")
loop = 4
elif Command_Line.get().lower() == ("e"):
update_textbox("""-----------------------------------------------------------------------------------
There is a door that is boarded up, it is not possible to remove them.\n""")
loop = 4
elif Command_Line.get().lower() == ("go east"):
update_textbox("""-----------------------------------------------------------------------------------
There is a door that is boarded up, it is not possible to remove them.\n""")
loop = 4
elif Command_Line.get().lower() == ("open door"):
update_textbox("""-----------------------------------------------------------------------------------
It is not possible to open the door.\n""")
loop = 4
elif Command_Line.get().lower() == ("remove boards"):
update_textbox("""-----------------------------------------------------------------------------------
The boards are fastened securely, we cannot physically remove them!\n""")
loop = 4
elif Command_Line.get().lower() == ("look at house"):
update_textbox("""-----------------------------------------------------------------------------------
The house is desolate and abandoned, it may have been used by the resistance as an outpost many moons ago.\n""")
loop = 4
elif Command_Line.get().lower() == ("read file"):
update_textbox("""-----------------------------------------------------------------------------------
The file says: 'INGSOC; MoP mission to obtain lost mission files from the 'Resistance.'
The file name is labelled 'Op Banner II - MoP reclimation of mission files.'""")
loop = 4
elif Command_Line.get().lower() == ("jump"):
update_textbox("""-----------------------------------------------------------------------------------
How High?\n""")
loop = 4
elif Command_Line.get().lower() == ("south"):
loop = 8
elif Command_Line.get().lower() == ("go south"):
loop = 8
elif Command_Line.get().lower() == ("s"):
loop = 8
else:
update_textbox("""-----------------------------------------------------------------------------------
Command Unknown. For control help please type 'help'.\n""")
loop = 4
# this bind() method will allow us to submit our command for processing.
Command_Line.bind("<Return>", process_commands)
start_game()
root.mainloop()