如何在执行使用 pyinstaller 创建的 exe 文件时在我的 tkinter 窗口而不是 CMD 窗口上获取 python 控制台日志 [英] How to get python console logs on my tkinter window instead of a CMD window while executing an exe file created using pyinstaller

查看:48
本文介绍了如何在执行使用 pyinstaller 创建的 exe 文件时在我的 tkinter 窗口而不是 CMD 窗口上获取 python 控制台日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 python 3.6 脚本,它执行一些数据转换并使用 tkinter 进行 GUI(文件夹选择和其他选项).

I have created a python 3.6 script that performs some data conversion and uses tkinter for GUI (folder selection and other options).

我已使用 pyinstaller 将其转换为 exe 文件 &希望其他用户(没有安装 python)能够使用该工具.

I have converted this to an exe file using pyinstaller & would like other users (who don't have python installed) to be able to use the tool.

但是,当我打开 exe 时,它会打开一个 CMD 窗口,其中显示通常显示在 python 控制台 上的日志.

However, when I open the exe, it opens a CMD window which shows the logs usually shown on the python console.

我想将其重定向到 tkinter 窗口本身中的 text boxframe - 而不是打开新的 CMD单击时窗口.

I'd like to get this redirected to a text box or frame in my tkinter window itself - instead of opening a new CMD window when clicked.

import tkinter as tk
from tkinter import filedialog as fd

def browse():
    directory=fd.askdirectory()
    print ('The selected  directory is: ', directory)

def convert():
    # perform file manipulation
    print ("Files converted")

window = tk.Tk()
window.title("Title")
label=tk.Label(window,text="Instructions")
label.pack()
browseButton=tk.Button(window,text="Browse Folder", command=browse)
browseButton.pack(pady=10)
runButton=tk.Button(window,text="Convert files", command=convert)
runButton.pack(pady=10)
window.mainloop()

然后我使用 pyinstaller 将文件转换为 exe

> pyinstaller --onefile TkinterGUI_test.py

预期结果

我在 stackoverflow 上看到了许多相关但不符合我的要求的帖子.任何帮助将不胜感激.谢谢!:)

I have seen numerous posts on stackoverflow which are related but don't match my requirements. Any help would be highly appreciated. Thanks! :)

推荐答案

要隐藏控制台,您需要将 --noconsole 添加到您的 pyinstaller 命令中.

to hide the console you need to add the --noconsole to your pyinstaller command.

为了重定向您的打印输出,您可以使用以下内容:

in order to redirect your printed output you could use something like this:

import tkinter as tk
import sys

class PrintLogger(): # create file like object
    def __init__(self, textbox): # pass reference to text widget
        self.textbox = textbox # keep ref

    def write(self, text):
        self.textbox.insert(tk.END, text) # write text to textbox
            # could also scroll to end of textbox here to make sure always visible

    def flush(self): # needed for file like object
        pass

if __name__ == '__main__':
    def do_something():
        print('i did something')
        root.after(1000, do_something)

    root = tk.Tk()
    t = tk.Text()
    t.pack()
    # create instance of file like object
    pl = PrintLogger(t)

    # replace sys.stdout with our object
    sys.stdout = pl

    root.after(1000, do_something)
    root.mainloop()

因为 print 语句通过替换我们在文本框中得到的输出将其输出定向到 sys.stdout,这意味着 print 插入换行符以及它通常在终端中执行的任何其他操作.

because the print statement directs its output at sys.stdout by replacing the output we get exactly the same in the textbox, this means that print inserts newlines and anything else it would normally do in the terminal.

这篇关于如何在执行使用 pyinstaller 创建的 exe 文件时在我的 tkinter 窗口而不是 CMD 窗口上获取 python 控制台日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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