快速工程摘要 :使用Tkinter制作一个python小部件,显示来自多个json和txt文件的数据。 需要在Windows中工作。 我在哪里 :一切都很好,与JSON文件。 但是我遇到了txt文件的麻烦。 我可以用这段代码parsing出我需要的信息:
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中的所有数据而不是单独的小部件。
我想简单地将所有这些信息从命令shell移动到文本框小部件(或者一个tkmessage小部件,但我觉得文本框会更合适)。 一个非常长的谷歌search过程给了我很多代码不起作用 – 任何提示? 谢谢!
注:这不是所有的代码 – 只是我需要帮助修复的部分
什么是一个很好的方法来打包Windows XP的GTK Python应用程序?
string安全function与安全增强CRT
链接到HTML中的UNC /本地资源
使用自动换行创build可resize/多行Tkinter / ttk标签
PEB结构中的BeingDebugged标志的目的是什么?
Windows窗体devise器重命名复制/粘贴控件
Windows密码和组
包含无法从Windows中看到的文件
什么是从malloc()做的Windows和Linux本地操作系统/系统调用?
在string中使用转义空格来反转path
这是我想要的。 您希望您的应用程序打开文件并将其解析出来。 对于每个解析的行,您都希望它将文本(或附加文本)插入到文本控件中。 我会为每个文件类型创建一个方法来解析。 然后,我会遍历每个文件,并适当调用解析器。 当你完成解析,你可以打电话
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()
# 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")
我喜欢的一个资源是http://effbot.org/tkinterbook/text.htm 。 不幸的是,从文本到工作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.")
http://tkinter.unpythonic.net/wiki/CmdTk
在这里,你去这是你想要的,这里的所有其他答案是从这里直接复制粘贴。 和大多数不工作的XD。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。