在文本框中显示打印输出
displaying print output in a textbox
我在 tkinter 中创建了一个 GUI,当我点击 Get 时,它必须产生在我的编码中使用 Print 语句显示的输出结果,必须显示在文本框中以及同一内部的滚动条gui,一旦我点击。
我的代码:-
import Tkinter as tk
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.L1 = tk.Label(self, text="Enter the query")
self.L1.pack()
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Get", command=self.on_button)
self.button.pack()
self.entry.pack()
def on_button(self):
s1=(self.entry.get())
with open("myxml.txt","rb")as f:
row = f.readlines()
for i, r in enumerate(row):
if s1 in r:
for x in range(i-3,i+4):
file1.write(row[x] + '\n')
s = row[x]
m = re.sub(r'<(\w+)\b[^>]*>([^<]*)</>', r'-', s)
print(re.sub(r'<[^<>]*>','', m)) #how to display it in textbox inside the same gui
app = SampleApp()
app.mainloop()
请帮助我在文本框中实现输出!提前致谢!
您需要的小部件是 tk.Text。您可以在 self.init:
中使用以下行创建小部件
self.text = tk.Text(self)
self.text.pack()
然后您可以使用 insert method 向其中添加文本。文本小部件跟踪光标,您必须说出将文本插入其中的位置。例如,要在框的末尾插入一些文本,您可以使用:
self.text.insert(tk.END, "Some text here")
或在您的程序中:
self.text.insert(tk.END, re.sub(r'<[^<>]*>','', m))
您还可以使用 tkinter.scrolledtext 中的 ScrolledText 而不是文本小部件。这使您可以免费获得滚动条。导入它:
import tkinter.scrolledtext as tkst
并在 init 中实例化小部件:
self.text = tkst.ScrolledText(self)
我在 tkinter 中创建了一个 GUI,当我点击 Get 时,它必须产生在我的编码中使用 Print 语句显示的输出结果,必须显示在文本框中以及同一内部的滚动条gui,一旦我点击。
我的代码:-
import Tkinter as tk
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.L1 = tk.Label(self, text="Enter the query")
self.L1.pack()
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Get", command=self.on_button)
self.button.pack()
self.entry.pack()
def on_button(self):
s1=(self.entry.get())
with open("myxml.txt","rb")as f:
row = f.readlines()
for i, r in enumerate(row):
if s1 in r:
for x in range(i-3,i+4):
file1.write(row[x] + '\n')
s = row[x]
m = re.sub(r'<(\w+)\b[^>]*>([^<]*)</>', r'-', s)
print(re.sub(r'<[^<>]*>','', m)) #how to display it in textbox inside the same gui
app = SampleApp()
app.mainloop()
请帮助我在文本框中实现输出!提前致谢!
您需要的小部件是 tk.Text。您可以在 self.init:
中使用以下行创建小部件self.text = tk.Text(self)
self.text.pack()
然后您可以使用 insert method 向其中添加文本。文本小部件跟踪光标,您必须说出将文本插入其中的位置。例如,要在框的末尾插入一些文本,您可以使用:
self.text.insert(tk.END, "Some text here")
或在您的程序中:
self.text.insert(tk.END, re.sub(r'<[^<>]*>','', m))
您还可以使用 tkinter.scrolledtext 中的 ScrolledText 而不是文本小部件。这使您可以免费获得滚动条。导入它:
import tkinter.scrolledtext as tkst
并在 init 中实例化小部件:
self.text = tkst.ScrolledText(self)