gpt4 book ai didi

Python tkinter : Make any output appear in a text box on GUI not in the shell

转载 作者:太空狗 更新时间:2023-10-29 18:32:58 25 4
gpt4 key购买 nike

我正在使用 python 和 tkinter 制作一个 GUI,只是想知道是否有办法让任何输出文本出现在 GUI 的窗口中而不是解释器/shell 上?

提前致谢

最佳答案

如果按照 Bryan Oakley 的评论中的建议,您想要“在您的 GUI 中打印‘foo’,但让它神奇地出现在文本小部件中”,请参阅上一个问题的答案 Python : Converting CLI to GUI .这个答案解决了如何在文本框中生成输出这一更简单的问题。要生成滚动文本窗口,请创建并放置或打包一个文本小部件(我们称它为 mtb),然后使用像 mtb.insert(Tkinter.END, ms) 这样的命令将字符串 ms 添加到文本框 mtb 中,并喜欢 mtb.see(Tkinter.END) 使框滚动。 (有关详细信息,请参阅“The Tkinter Text Widget ”文档。)例如:

#!/usr/bin/env python
import Tkinter as tk

def cbc(id, tex):
return lambda : callback(id, tex)

def callback(id, tex):
s = 'At {} f is {}\n'.format(id, id**id/0.987)
tex.insert(tk.END, s)
tex.see(tk.END) # Scroll if necessary

top = tk.Tk()
tex = tk.Text(master=top)
tex.pack(side=tk.RIGHT)
bop = tk.Frame()
bop.pack(side=tk.LEFT)
for k in range(1,10):
tv = 'Say {}'.format(k)
b = tk.Button(bop, text=tv, command=cbc(k, tex))
b.pack()

tk.Button(bop, text='Exit', command=top.destroy).pack()
top.mainloop()

请注意,如果您希望文本窗口长时间保持打开状态和/或积累千兆字节的文本,或许可以跟踪文本框中的数据量,然后使用 delete 方法每隔一段时间限制它。

关于Python tkinter : Make any output appear in a text box on GUI not in the shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14879916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com