【问题标题】:Print CMD results into a GUI text box tkinter将 CMD 结果打印到 GUI 文本框 tkinter
【发布时间】:2017-08-03 12:57:11
【问题描述】:

我想在办公室分发我的程序,以便其他人可以在我不在时使用它

我的代码:

from tkinter import *
import os


top = Tk()
top.wm_title("testest")
top.minsize(width=300, height=150)
top.maxsize(width=300, height=150)

def runscript():
    os.system('python test.py')

B = Tk.Button(top, text = 'Run test', command = runscript)
B.config(width=15, height=1)
B.pack()
top.mainloop()

有什么方法可以让我在 .py 文件中的打印功能在 GUI 文本框而不是命令行中打印出来?

【问题讨论】:

  • 是的,有。但是修改“test.py”会好多,这样它就可以导入到你的 Tkinter 脚本中,而不是使用os.system 运行它。
  • 修改难度如何?我不擅长 tkinter,这是一种快速的方法,可以让我有一个“开始”按钮来运行我的脚本,该脚本是永久设置的。
  • 如果 test.py 只有一个 print 调用,那么它很容易:而不是调用 print,你调用一个更新 Tkinter 小部件文本的函数(通过它的 .config 方法)。您可能可以为此使用 Label 小部件。
  • 不,我有几张照片。 '当前正在传输的文件'、'当前正在合并所有 pdfs'、'pdfs 已合并'。你认为将其留在命令行中更容易吗?

标签: python tkinter


【解决方案1】:

这是一个粗略的演示,展示了如何将文本字符串打印到标签。请注意,长字符串不会被包装。如果需要,您必须自己通过插入 \n 换行符来包装字符串,或者使用 wraplength 选项,如 Label widget 文档中所述。或者,您可以使用 Text widget 而不是 Label 来显示文本。

我的test 函数模拟了“test.py”脚本中代码的操作。

import tkinter as tk
from time import sleep

# A dummy `test` function
def test():
    # Delay in seconds
    delay = 2.0
    sleep(delay)
    print_to_gui('Files currently transferring')
    sleep(delay)
    print_to_gui('Currently merging all pdfs')
    sleep(delay)
    print_to_gui('PDFs have been merged')
    sleep(delay)
    print_to_gui('Finished!\nYou can click the "Run test"\n'
        'button to run the test again.')

# Display a string in `out_label`
def print_to_gui(text_string):
    out_label.config(text=text_string)
    # Force the GUI to update
    top.update()

# Build the GUI
top = tk.Tk()
top.wm_title("testest")
top.minsize(width=300, height=150)
top.maxsize(width=300, height=150)

b = tk.Button(top, text='Run test', command=test)
b.config(width=15, height=1)
b.pack()

# A Label to display output from the `test` function
out_label = tk.Label(text='Click button to start')
out_label.pack()

top.mainloop()

请注意,我们通常在 GUI 程序中调用 time.sleep,因为它会导致整个程序在睡眠发生时冻结。我在这里用它来模拟当你的真实代码执行它的处理时会发生的阻塞延迟。

在不阻止正常 GUI 处理的 Tkinter 程序中进行延迟的常用方法是使用.after method


您会注意到我用import tkinter as tk 替换了您的from tkinter import *。这意味着我们需要输入例如tk.Label 而不是Label,但这使代码更易于阅读,因为我们现在知道所有不同名称的来源。这也意味着我们不会用 tkinter 定义的所有名称来淹没我们的命名空间。 import tkinter as tk 只是在命名空间中添加了 1 个名称,from tkinter import * 在当前版本中添加了 136 个名称。请参阅Why is “import *” bad? 了解有关此重要主题的更多信息。


这是一个稍微花哨的示例,它将新文本附加到当前的标签文本中,并带有自动换行。它还有一个函数clear_label 可以从标签中删除所有文本。它不是直接将文本存储在标签中,而是使用StringVar。这使得访问旧文本更容易,以便我们可以附加到它。

import tkinter as tk
from time import sleep

# A Dummy `test` function
def test():
    # Delay in seconds
    delay = 1.0

    clear_label()
    print_to_label('Files currently transferring')
    sleep(delay)
    print_to_label('Currently merging all pdfs')
    sleep(delay)
    print_to_label('PDFs have been merged')
    sleep(delay)
    print_to_label('\nFinished!\nYou can click the "Run test" '
        'button to run the test again. '
        'This is a very long string to show off word wrapping.'
    )

# Append `text_string` to `label_text`, which is displayed in `out_label`
def print_to_label(text_string):
    label_text.set(label_text.get() + '\n' + text_string)
    # Force the GUI to update
    top.update()

def clear_label():
    label_text.set('')
    top.update()

# Build the GUI
top = tk.Tk()
top.wm_title("testest")
top.minsize(width=300, height=150)
top.maxsize(width=300, height=350)

b = tk.Button(top, text='Run test', command=test)
b.config(width=15, height=1)
b.pack()

# A Label to display output from the `test` function
label_text = tk.StringVar()
out_label = tk.Label(textvariable=label_text, wraplength=250)
label_text.set('Click button to start')
out_label.pack()

top.mainloop()

【讨论】:

  • 看,我不明白当函数实际完成时我如何打印出我的打印语句。添加 print_to_label('Currently merging all pdfs') 没有任何帮助,因为我不知道该过程何时真正完成。
  • @uniqueusername42o 抱歉。我只是注意到我的答案中的一些信息有点误导,所以我把它删除了。我的print_to_label 函数不是print 函数的完美替代品,因为它只接受一个字符串参数。但是,如果您的 print 在“test.py”中调用只打印一个字符串,那么您可以将它们替换为对 print_to_label 的调用。之前我说过你可以将“test.py”导入 Tkinter 脚本,但仔细想想,创建一个包含我的 GUI 代码的新版本“test.py”可能更简单。
  • @uniqueusername42o 如果您仍然不确定该怎么做,请修改您的问题,使其包含“test.py”的虚拟版本,我将向您展示如何向其中添加 GUI 内容.我们不需要查看完整的“test.py”,包含所有 PDF 内容,只需一个最小版本,以便您了解如何将其与 GUI 代码联系在一起。
猜你喜欢
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
相关资源
最近更新 更多