连接到按钮的 Python 编译器 [英] Python compiler connected to a button

查看:55
本文介绍了连接到按钮的 Python 编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将 Python 编译器连接到一个名为 run 的按钮?将 PQT4 用于 Python 3,我有一个运行按钮和一个文本编辑器,当用户单击运行时,我希望检查和编译文本编辑器中的所有代码.这可能吗?有没有类似的代码示例?

Is it possible to connect the Python compiler to a single button called run? Using PQT4 for Python 3, I have a run button, and a text editor, when the user clicks run I would like all the code in the text editor to be checked and complied. Is this possible? Are there any code examples for something like this?

提前致谢!

推荐答案

您是否尝试过在交互式解释器实例中运行代码?交互式控制台对象

Have you tried to run the code in an interactive interpreter instance? Interactive Console Objects

交互控制台对象说明

当您导入类并创建一个新实例时,您就可以在不中断 Python 主线程的情况下运行代码.

When you import the class and create a new instance you could then run code without interrupting the main python thread.

from code import InteractiveInterpreter

code1 = """
def foo():
    print notDefined

foo()
"""

code2 = """
def baz(spam):
    print spam

baz('eggs')
"""

interpreter = InteractiveInterpreter()
interpreter.runcode(code1)
interpreter.runcode(code2)

输出:

Traceback (most recent call last):
  File "<string>", line 5, in <module>
  File "<string>", line 3, in foo
NameError: global name 'notDefined' is not defined
eggs

这篇关于连接到按钮的 Python 编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆