如何让 sys.stdout 将结果打印到 tkinter GUI 文本框中
How to get sys.stdout to print results into a tkinter GUI textbox
我正在从 MySQL 数据库中获取所有行,并试图让它们在 textbox
中打印,并且还在我的 table_frame
中使用 sys.stdout
。
我的打印输出还没有正确设置间距格式。我正在尝试先将打印输出放入 textbox
。
我尝试像 Whosebug 所说的那样突出显示代码,但它不会突出显示预览中的文本。对不起,如果它更难阅读。也许它会在 post.
时自行修复
感谢您的帮助。
from tkinter import *
import tkinter.messagebox
import mysql.connector
# ======================MySQL Connection================================================================
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
passwd = "<INSERT PASSWORD>",
database = "testdb",
)
# Create Cursor Instance
my_cursor = mydb.cursor()
table_frame = Frame(root, width=500, height=140, bg='yellow')
table_frame.pack(side=TOP)
textbox=Text(table_frame)
textbox.pack()
row = []
def inv_row_print():
print("Item Code\tBrand\t\tUnits\t\tIn Stock\t\tUnit Cost")
my_cursor.execute("SELECT * FROM trialprojectdb.inventory")
all_rows = my_cursor.fetchall()
for row in all_rows:
result = print("{0}\t\t{1}\t\t{2}\t\t{3}\t\t{4}".format(row[0], row[1], row[2],
row[3], row[4]))
textbox.insert(0, result)
sys.stdout.write = inv_row_print()
btn2 = Button(btm_frame, text='Show Table', command = lambda : inv_row_print())
btn2.place(x=200,y=90)
root.mainloop()
这是我得到的反馈。
Item Code Brand Units In Stock Unit Cost
1 M&M Peanut 62 None 9.98
Traceback (most recent call last):
File "Import_Test_Code2.py", line 124, in <module>
sys.stdout.write = inv_row_print()
File "Import_Test_Code2.py", line 123, in inv_row_print
textbox.insert(0, result)
File "C:\Users\darre\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3269, in insert
self.tk.call((self._w, 'insert', index, chars) + args)
_tkinter.TclError: wrong # args: should be ".!frame3.!text insert index chars ?tagList chars tagList ...?"
以防万一其他人遇到这个问题,我将 post @furas
帮助我得到的解决方案。
Original
for row in all_rows:
result = print("{0}\t\t{1}\t\t{2}\t\t{3}\t\t{4}".format(row[0], row[1], row[2],
row[3], row[4]))
textbox.insert(0, result)
New - removed print()
from result
and in textbox.insert
changed the index from 0
to 1.0
. Also, deleted sys.stdout.write = inv_row_print()
.
for row in all_rows:
result = "{0}\t\t{1}\t\t{2}\t\t{3}\t\t{4}".format(row[0], row[1], row[2],
row[3], row[4])
textbox.insert(1.0, result)
我正在从 MySQL 数据库中获取所有行,并试图让它们在 textbox
中打印,并且还在我的 table_frame
中使用 sys.stdout
。
我的打印输出还没有正确设置间距格式。我正在尝试先将打印输出放入 textbox
。
我尝试像 Whosebug 所说的那样突出显示代码,但它不会突出显示预览中的文本。对不起,如果它更难阅读。也许它会在 post.
时自行修复感谢您的帮助。
from tkinter import *
import tkinter.messagebox
import mysql.connector
# ======================MySQL Connection================================================================
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
passwd = "<INSERT PASSWORD>",
database = "testdb",
)
# Create Cursor Instance
my_cursor = mydb.cursor()
table_frame = Frame(root, width=500, height=140, bg='yellow')
table_frame.pack(side=TOP)
textbox=Text(table_frame)
textbox.pack()
row = []
def inv_row_print():
print("Item Code\tBrand\t\tUnits\t\tIn Stock\t\tUnit Cost")
my_cursor.execute("SELECT * FROM trialprojectdb.inventory")
all_rows = my_cursor.fetchall()
for row in all_rows:
result = print("{0}\t\t{1}\t\t{2}\t\t{3}\t\t{4}".format(row[0], row[1], row[2],
row[3], row[4]))
textbox.insert(0, result)
sys.stdout.write = inv_row_print()
btn2 = Button(btm_frame, text='Show Table', command = lambda : inv_row_print())
btn2.place(x=200,y=90)
root.mainloop()
这是我得到的反馈。
Item Code Brand Units In Stock Unit Cost
1 M&M Peanut 62 None 9.98
Traceback (most recent call last):
File "Import_Test_Code2.py", line 124, in <module>
sys.stdout.write = inv_row_print()
File "Import_Test_Code2.py", line 123, in inv_row_print
textbox.insert(0, result)
File "C:\Users\darre\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3269, in insert
self.tk.call((self._w, 'insert', index, chars) + args)
_tkinter.TclError: wrong # args: should be ".!frame3.!text insert index chars ?tagList chars tagList ...?"
以防万一其他人遇到这个问题,我将 post @furas
帮助我得到的解决方案。
Original
for row in all_rows:
result = print("{0}\t\t{1}\t\t{2}\t\t{3}\t\t{4}".format(row[0], row[1], row[2],
row[3], row[4]))
textbox.insert(0, result)
New - removed
print()
fromresult
and intextbox.insert
changed the index from0
to1.0
. Also, deletedsys.stdout.write = inv_row_print()
.
for row in all_rows:
result = "{0}\t\t{1}\t\t{2}\t\t{3}\t\t{4}".format(row[0], row[1], row[2],
row[3], row[4])
textbox.insert(1.0, result)