如何在 Tkinter 中运行 python 文件 [英] How to run python file in Tkinter

查看:102
本文介绍了如何在 Tkinter 中运行 python 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 的初学者,因此这个问题.我想在 Tkinter 中运行一个 python 文件(smileA.py).我将如何开始?

I'm a beginner in Python, hence the question. i would like to run a python file (smileA.py) in Tkinter. How would i start?

我不希望它在单击按钮时运行,但文件在 Tkinter 本身中自动运行.

I do not wish for it to run when clicking a button, but the file to run automatically in the Tkinter itself.

谢谢.

推荐答案

您需要一个名为 subprocess 的模块,它允许您使用 Popen 从 Python 代码中运行文件.

What you need a module called subprocess, which will allow you to run files, from within your python code, by using Popen.

导入模块后,运行文件的代码如下所示:

The code for running the file will look something like this, once you import the module:

from subprocess import Popen

p = Popen("smileA.py")
stdout, stderr = p.communicate()

如果这就是在 Tkinter 中运行它"的意思,例如,运行该代码,同时在 Tkinter 中在同一文件中构建 GUI.

If that's what you mean by "Running it in Tkinter", as in, running that code, while building a GUI in Tkinter in the same file.

这篇关于如何在 Tkinter 中运行 python 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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