Python 使用tkinter获取要在小部件中显示的命令窗口输出
快速项目摘要:使用Tkinter制作一个python小部件,显示来自多个json和txt文件的数据。需要在Windows中工作。 我现在的位置:json文件一切都很顺利。但是我遇到了txt文件的麻烦。我可以使用以下代码从必要的文件中解析所需的信息:Python 使用tkinter获取要在小部件中显示的命令窗口输出,python,windows,python-2.7,tkinter,Python,Windows,Python 2.7,Tkinter,快速项目摘要:使用Tkinter制作一个python小部件,显示来自多个json和txt文件的数据。需要在Windows中工作。 我现在的位置:json文件一切都很顺利。但是我遇到了txt文件的麻烦。我可以使用以下代码从必要的文件中解析所需的信息: from Tkinter import * import re results = open("sample_results.txt", "r") for line in results: if re.match("(.*)test(.
from Tkinter import *
import re
results = open("sample_results.txt", "r")
for line in results:
if re.match("(.*)test(.*)", line):
print line
if re.match("(.*)number(.*)", line):
print line
if re.match("(.*)status(.*)", line):
print line
if re.match("(.*)length(.*)", line):
print line
问题:它显示命令外壳中的所有数据,而不是在单独的小部件中
我只想将所有这些信息从命令shell移动到文本框小部件(或tkmessage小部件,但我觉得文本框更合适)。一个很长的谷歌搜索过程给了我很多不起作用的代码——有什么提示吗?谢谢
注意:这不是全部代码-只是我需要帮助修复的部分。一种方法是使用简单的tkinter标签:
# somewhere in your main class, I suppose:
self.log_txt = tkinter.StringVar()
self.log_label = tkinter.Label(self.inputframe, textvariable=self.log_txt, justify=tkinter.LEFT)
self.log_label.pack(anchor="w")
然后,一种非常简单的将文本放入标签的方法:
def log(self, s):
txt = self.log_txt.get() + "\n" + s
self.log_txt.set(txt)
或者,您可以使用tkinter.Text小部件。在这种情况下,可以使用insert方法插入文本:
self.textbox = tkinter.Text(parent)
self.textbox.insert(tkinter.END, "some text to insert")
我喜欢的一个资源是。不幸的是,很难从该文本转换为可工作的Python代码:(下面是一个带有丑陋的tkinter GUI的小示例程序,它将文本添加到文本框中:
#!/usr/bin/env python
try:
import tkinter
except ImportError:
import Tkinter as tkinter
import _tkinter
import platform
class TextBoxDemo(tkinter.Tk):
def __init__(self, parent):
tkinter.Tk.__init__(self, parent)
self.parent = parent
self.wm_title("TextBoxDemo")
self.textbox = tkinter.Text(self)
self.textbox.pack()
self.txt_var = tkinter.StringVar()
self.entry = tkinter.Entry(self, textvariable=self.txt_var)
self.entry.pack(anchor="w")
self.button = tkinter.Button(self, text="Add", command=self.add)
self.button.pack(anchor="e")
def add(self):
self.textbox.insert(tkinter.END, self.txt_var.get())
if __name__ == '__main__':
try:
app = TextBoxDemo(None)
app.mainloop()
except _tkinter.TclError as e:
if platform.system() == 'Windows':
print(e)
print("Seems tkinter will not run; try running this program outside a virtualenv.")
以下是我认为您需要的。您希望应用程序打开文件并解析它们。对于每个解析的行,您希望它插入文本(或附加文本)到文本控件。我将为每个文件类型创建一个方法来进行解析。然后我将在每个文件上循环并根据需要调用解析器。解析完成后,可以调用
self.textbox.insert(tkinter.END, parsed_text)
另一种方法是将stdout重定向到文本控件,然后打印解析后的行。我发现后一种方法更灵活,尤其是当我想用子进程调用一个单独的程序并逐位读取其输出时。使用Tkinter有一种方法:
import ScrolledText
import sys
import tkFileDialog
import Tkinter
########################################################################
class RedirectText(object):
""""""
#----------------------------------------------------------------------
def __init__(self, text_ctrl):
"""Constructor"""
self.output = text_ctrl
#----------------------------------------------------------------------
def write(self, string):
""""""
self.output.insert(Tkinter.END, string)
########################################################################
class MyApp(object):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
self.root = parent
self.root.title("Redirect")
self.frame = Tkinter.Frame(parent)
self.frame.pack()
self.text = ScrolledText.ScrolledText(self.frame)
self.text.pack()
# redirect stdout
redir = RedirectText(self.text)
sys.stdout = redir
btn = Tkinter.Button(self.frame, text="Open file", command=self.open_file)
btn.pack()
#----------------------------------------------------------------------
def open_file(self):
"""
Open a file, read it line-by-line and print out each line to
the text control widget
"""
options = {}
options['defaultextension'] = '.txt'
options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
options['initialdir'] = '/home'
options['parent'] = self.root
options['title'] = "Open a file"
with tkFileDialog.askopenfile(mode='r', **options) as f_handle:
for line in f_handle:
print line
#----------------------------------------------------------------------
if __name__ == "__main__":
root = Tkinter.Tk()
root.geometry("800x600")
app = MyApp(root)
root.mainloop()
此代码可以正常工作,但无法回答我的问题。无论如何,谢谢。由于某种原因,我无法获得您的第一个建议
self.textbox.insert(tkinter.END,parsed_text
),以便正确工作,但您的“更灵活”方法工作得非常出色。非常感谢您的帮助!