Tkinter GUI协助

时间:2018-03-12 13:44:59

标签: python tkinter project

几周前我曾问过一些帮助,但被告知在我的问题或要求中我太模糊了。 我正在为我的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

1 个答案:

答案 0 :(得分:0)

Tkinter是一个非常易于使用的GUI库。根据您在问题中的评论判断,您已尝试但未能理解Tkinter的基础知识。您可能想知道,如果您创建mainloop然后在mainloop之后编写代码,它将不会在您的GUI中使用。主循环需要在程序结束时进行。

我们不会使用inputprint,而是使用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()