简体   繁体   English

如何在当前的python代码中使用GUI / Window?

[英]How can I use a GUI/Window with my current python code?

I have followed a tutorial on making a simple text based game in Python. 我遵循了有关使用Python制作基于文本的简单游戏的教程。 I'm going to use what I learned from it to make a post-apocalyptic text adventure. 我将利用从中学到的知识来进行世界末日后的文字冒险。 Everything works, but I don't really want to use just the command console as the game. 一切正常,但是我真的不想只将命令控制台用作游戏。 Instead, I want to use a window, which I know can be done with Tkinter. 相反,我想使用一个窗口,我知道可以用Tkinter完成。 I just don't know how. 我就是不知道

What I'm asking is if there's a way to add a GUI or window to my existing functions. 我要问的是是否可以将GUI或窗口添加到现有功能中。 The code is below: 代码如下:

    #A simple text-based game test

global table
table=0

def start():
    print 'Welcome'
    global gold
    gold=0
    lobby()

def lobby():
    print 'You are in the lobby.'
    command=prompt()

    if command=='north':
        bedroom()
    elif command=='gold':
        currentGold()
        lobby()
    elif command=='end':
        return
    else:
        lobby()

def prompt():
    x=raw_input('Type a command: ')
    return x

def currentGold():
    global gold
    print 'current gold: ', gold

def bedroom():
    global gold, table
    print 'You are in the bedroom'
    command=prompt()
    if command=='south':
        lobby()
    elif command=='bed':
        print 'You return to your bed and find nothing'
        bedroom()
    elif command=='table':
        if table==0:
            print 'You go to the table and find 50 gold'
            gold=gold+50
            table=1
            bedroom()
        else:
            print 'There is nothing else on the table'
            bedroom()
    elif command=='gold':
        currentGold()
        bedroom()
    elif command=='end':
        return
    else:
        bedroom()

start()

Basically, you start in a lobby, and then explore a bedroom (not really, it's just a simple test). 基本上,您从大厅开始,然后探索一间卧室(不是真的,这只是一个简单的测试)。 I'd appreciate anyone's help or input. 感谢任何人的帮助或投入。

In general, a GUI program has to be rewritten as an event loop, rather than just a sequence of code. 通常,必须将GUI程序重写为事件循环,而不仅仅是一系列代码。

For example, if you write a function which just waits forever for input, then the entire GUI is waiting forever for input, which means you can't respond to mouse drags or anything else, and your window manager will display a beachball or pop up a "dead program" dialog or in some other way alert the user that your program is "frozen". 例如,如果您编写的函数只是永远等待输入,则整个GUI都会永远等待输入,这意味着您无法响应鼠标拖动或其他任何操作,并且窗口管理器将显示一个沙滩球或弹出窗口“无效程序”对话框或以其他方式提醒用户程序已“冻结”。

Instead, you have to write a function which just puts up an input dialog, attaches a handler or callback that gets run when the input comes in to the dialog, and then returns. 相反,您必须编写一个函数,该函数只建立一个输入对话框,将在输入进入对话框时运行的处理程序或回调附加到对话框,然后返回。

So, code that looks like this: 因此,代码如下所示:

def lobby():
    print 'You are in the lobby'
    command=prompt()
    if command == 'north':
        bedroom()
    elif command=='gold':
        currentGold()
        lobby()
    elif command=='end':
        return
    else:
        lobby()

… has to be split in half, like this: ……必须分成两半,如下所示:

def lobby():
    display_text('You are in the lobby')
    prompt_window = PromptWindow(handler = lobby_handler)
    prompt_window.show()

def lobby_handler(command):
    if command == 'north':
        bedroom()
    elif command=='gold':
        currentGold()
        lobby()
    elif command=='end':
        return
    else:
        lobby()

If this doesn't make sense, you probably want to follow some tutorials to build some simple GUI apps first, and only then come back to converting your existing program into a GUI app. 如果这没有意义,则您可能想先阅读一些教程,以构建一些简单的GUI应用程序,然后再返回,将现有程序转换为GUI应用程序。


Just splitting functions in half is the quick & dirty way to turn sequential code into event-based code, but it isn't always the best. 将功能分成两半是将顺序代码转换为基于事件的代码的快速而肮脏的方法,但这并不总是最好的。 It's a great way to end up in "callback hell". 这是结束“回调地狱”的好方法。

For example, what if currentGold is popping up a dialog and waiting for the user to click it, and we don't want to go back to lobby until they click it? 例如,如果currentGold弹出一个对话框并等待用户单击它,而我们不希望在单击它们之前回到lobby ,该怎么办? The only way to make this work is for lobby_handler to pass lobby to currentGold , so currentGold can pass it to currentGoldHandler . 进行此工作的唯一方法是将lobby_handler传递到currentGold lobby ,因此currentGold可以将其传递给currentGoldHandler And what if currentGold_handler needs to access local variables from currentGold ? 如果currentGold_handler需要从currentGold访问局部变量,该怎么办? You have to define currentGold_handler locally so you can use it as a closure, or use functools.partial to bind them in. And so on. 您必须在本地定义currentGold_handler ,以便可以将其用作闭包,也可以使用functools.partial绑定它们。依此类推。 Before you know it, you've got code indented 60 characters, inconsistently using some callback-passing convention that you didn't design until you'd written 100 functions, 40 of which violate it in some subtle way. 在不知不觉中,您已经使代码缩进了60个字符,并不一致地使用了您在编写100个函数之前就没有设计的回调传递约定,其中40个函数以某种微妙的方式违反了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的基本 python 代码添加到 gui 中 - How can I add my basic python code into a gui 如何使我的GUI程序(tkinter)在没有安装python的计算机上工作,从而不会显示控制台窗口? - How can I make my GUI program(tkinter) work on computers with no python installed such that no console window appears? 如何将我的 python GUI 与我的 python 代码结合起来 - How do I combine my python GUI with my python code 我应该如何在应用程序中使用wxFormBuilder Python GUI代码? - How am I supposed to use wxFormBuilder Python GUI code in my applications? 我如何在Python的gui window列表框中显示课程? - How can i display courses in the listbox of gui window in Python? 如何在 Mac 上使用 python 显示 gui 窗口 - How can I show a gui window with python on Mac Python:如何使用 for 循环执行我的代码 5 次? - Python: How can I use a for loop to execute my code 5 times? 如何在我的 python 除数代码中使用内置函数? - how can I use builtin function in my python divisor code? 我在我的代码中使用Jinja2“递归”标记,但是如何获得当前循环的深度? - I use Jinja2 “recursive” tag in my code,but how can I get the depth of current loop? 如何压缩此 Python Tkinter GUI 输入代码? - How can I condense this Python Tkinter GUI input code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM