在执行使用 pyinstaller 创建的 exe 文件时,如何在我的 tkinter window 而不是 CMD window 上获取 python 控制台日志
How to get python console logs on my tkinter window instead of a CMD window while executing an exe file created using pyinstaller
问题陈述
我创建了一个 python 3.6
脚本,它执行一些数据转换并使用 tkinter
用于 GUI(文件夹选择和其他选项)。
我已使用 pyinstaller
将其转换为 exe
文件,并希望其他用户(未安装 python 的用户)能够使用该工具。
但是,当我打开 exe
时,它会打开 CMD window
,其中显示通常显示在 python console
上的日志。
我想让它重定向到我的 tkinter
window 本身的 text box
或 frame
- 而不是打开一个新的 CMD window 单击时。
示例代码
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
预期结果
我在 Whosebug 上看到了很多相关但不符合我要求的帖子。任何帮助将不胜感激。谢谢! :)
要隐藏控制台,您需要将 --noconsole
添加到您的 pyinstaller 命令。
为了重定向您的打印输出,您可以使用这样的东西:
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
插入换行符以及它通常会在终端。
问题陈述
我创建了一个 python 3.6
脚本,它执行一些数据转换并使用 tkinter
用于 GUI(文件夹选择和其他选项)。
我已使用 pyinstaller
将其转换为 exe
文件,并希望其他用户(未安装 python 的用户)能够使用该工具。
但是,当我打开 exe
时,它会打开 CMD window
,其中显示通常显示在 python console
上的日志。
我想让它重定向到我的 tkinter
window 本身的 text box
或 frame
- 而不是打开一个新的 CMD window 单击时。
示例代码
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
预期结果
我在 Whosebug 上看到了很多相关但不符合我要求的帖子。任何帮助将不胜感激。谢谢! :)
要隐藏控制台,您需要将 --noconsole
添加到您的 pyinstaller 命令。
为了重定向您的打印输出,您可以使用这样的东西:
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
插入换行符以及它通常会在终端。