Python:创建另一个共享相同数据的 Python 控制台窗口
Posted
技术标签:
【中文标题】Python:创建另一个共享相同数据的 Python 控制台窗口【英文标题】:Python: Create another python console window sharing same data 【发布时间】:2013-05-21 03:50:57 【问题描述】:我正在尝试显示调试控制台窗口(需要与脚本共享数据),但主控制台仍处于打开状态。我知道如何做到这一点的唯一方法是执行第二个脚本,并与文件共享数据..
我希望第二个窗口是一个实际的控制台。
我使用的是 Windows 7,我的脚本不需要非常兼容。
【问题讨论】:
你的意思是:兼容=跨平台? 不需要兼容——应该在我的电脑上运行,但我不关心其他平台,所以兼容=跨平台 你可能想通过一些远程调试工具连接到进程。见***.com/questions/543196/… 【参考方案1】:如果您在 Windows 上,您可以使用 win32console 模块为您的子线程(因为您希望它们共享相同的数据)输出打开第二个控制台。如果您在 Windows 上,这是最简单、最简单的方法。
这是一个示例代码:
import win32console
import threading
from time import sleep
def child_thread():
win32console.FreeConsole() #Frees child_thread from using main console
win32console.AllocConsole() #Creates new console and all input and output of the child_thread goes to this new console
while True:
print("This is in child_thread console and the count is:",count)
#prints in new console dedicated to this thread
if __name__ == "__main__":
count = 0
threading.Thread(target=child_thread, args=[]).start()
while True:
count+=1
print("Hello from the main console - this is count:", count)
#prints in main console
sleep(1)
#and whatever else you want to do in ur main process
还有一种更好的方法来使用来自queue module 的队列,因为它可以让您更好地控制竞争条件。
这里是win32console module documentation
【讨论】:
【参考方案2】:您可以考虑在您的应用程序上使用 GUI 并弹出到将输入和输出(文本框)设计为控制台的窗口。从技术上讲,两个窗口将作为一个应用程序运行,因此可以访问相同的命名空间。要在 GUI 中包装 python,请查看 python Wx 和 Python TkInter。祝你好运。
PS 而且它会非常兼容:)
【讨论】:
谢谢,去看看 :)以上是关于Python:创建另一个共享相同数据的 Python 控制台窗口的主要内容,如果未能解决你的问题,请参考以下文章