将表格格式的文本打印到tk.Text小部件中,未按其预期的方式对齐 [英] Print tabular formated text into a tk.Text widget, not aligned as its supposed
问题描述
我无法在互联网上找到答案,所以希望您能为我提供帮助.我正在尝试从列表打印到Tkinter的文本框中.由于某种原因,当我将其打印到文本框中时,它并未按预期对齐,但在我将其打印到控制台时,其正确对齐了.
I couldn't found answer on the internet so I'm hoping you can help me. I'm trying to print from a list into a text box in Tkinter. From some reason, when I print it to text box it's not aligned as its supposed to be but when I print it to the console, its aligned correctly.
我正在使用的数据可以在数据中找到.
Data that I'm using you can find on data.
你们中的任何人都知道可能是什么问题吗?
Any of you knows what might be the problem?
from tkinter import *
popup = Tk()
popup.wm_title("Podaci mreze")
widthTabela = 600
heightTabela = 500
def zatvaranje():
popup.destroy()
screenw = popup.winfo_screenwidth()
screenh = popup.winfo_screenheight()
x = screenw / 2 - widthTabela / 2
y = screenh / 2 - heightTabela / 2
popup.geometry("%dx%d+%d+%d" % (widthTabela, heightTabela, x, y))
textTFrame = Frame(popup, borderwidth=1, relief="sunken")
textTabela = Text(textTFrame, width=83, height=28.4, wrap="none", borderwidth=0)
textTVSB = Scrollbar(textTFrame, orient="vertical", command=textTabela.yview)
textTHSB = Scrollbar(textTFrame, orient="horizontal", command=textTabela.xview)
textTabela.configure(yscrollcommand=textTVSB.set, xscrollcommand=textTHSB.set, font=("Arial", 10))
textTabela.config(state="disabled")
textTabela.grid(row=0, column=0, sticky="nsew")
textTVSB.grid(row=0, column=1, sticky="ns")
textTHSB.grid(row=1, column=0, sticky="ew")
textTFrame.grid(row=0, column=0)
file=open("D:\\Pycharm_Skripte\\IEEE9.txt","r")
listaPodataka = file.readlines()
textTabela.config(state="normal")
textTabela.delete('1.0', END)
for i in range(len(listaPodataka)):
print(listaPodataka[i])
textTabela.insert(INSERT, listaPodataka[i])
textTabela.config(state="disabled")
dugmeTabela=Button(popup, text="Close", command=zatvaranje).grid(row=2, column=0)
popup.mainloop()
当前结果:
推荐答案
问题:将表格格式的文本打印到tk.Text小部件中,但未按预期对齐.
在控制台中,其正确对齐.
Question: Print tabular formated text into a tk.Text widget, not aligned as its supposed.
In the console, its aligned correctly.
您必须使用固定宽度的字体,例如font=('Consolas', 10)
.
You have to use a fixed-width font, e.g. font=('Consolas', 10)
.
参考:
- Tkinter.Text.config-method
- 对话框Windows
- 对显示在
tk.Label
中的输出进行制表,而不会使数据失真./a> - text-alignment-issue-in-listbox
- Tkinter.Text.config-method
- Dialog Windows
- tabulate output, displayed in a
tk.Label
, without to distort the data. - text-alignment-issue-in-listbox
此示例显示了tkinter Dialog
中的用法:
This example shows the usage in a tkinter Dialog
:
import tkinter as tk
from tkinter import simpledialog
class TextDialog(simpledialog.Dialog):
def __init__(self, parent, data):
self.data = data
# super() returns at `.destroy()`
super().__init__(parent, "Podaci mreze")
def deiconify(self):
width, height = 600, 500
screenw, screenh = self.winfo_screenwidth(), self.winfo_screenheight()
x, y = screenw // 2 - width // 2, screenh // 2 - height // 2
self.geometry('{width}x{height}+{x}+{y}'.format(width=width, height=height, x=x, y=y))
super().deiconify()
def body(self, frame):
text = tk.Text(frame, font=('Consolas', 10), wrap="none", borderwidth=0)
text.grid(sticky='ewns')
text.insert('1.0', self.data)
return text
用法:
import tkinter as tk
import io
# Simulating tabulated file contents
IEEE9 = """ BusStatus R X Bc Ratio Pij_max Asngle
1.4.1 1 0 0.0576 0 1 250 0
3.6.1 1 0 0.0586 0 1 300 0
4.5.1 1 0.017 0.092 0.158 1 250 0
5.6.1 1 0.039 0.17 0.358 1 150 0
6.7.1 1 0.0119 0.1008 0.209 1 150 0
7.8.1 1 0.0085 0.072 0.149 1 250 0
8.2.1 1 0 0.0625 0 1 250 0
8.9.1 1 0.032 0.161 0.306 1 250 0
9.4.1 1 0.01 0.085 0.176 1 250 0
"""
class App(tk.Tk):
def __init__(self):
super().__init__()
# with open('IEEE9.txt') as fh:
with io.StringIO(IEEE9) as fh:
data = fh.read()
TextDialog(self, data)
if __name__ == "__main__":
App().mainloop()
使用Python测试:3.5-'TclVersion':8.6'TkVersion':8.6
Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6
这篇关于将表格格式的文本打印到tk.Text小部件中,未按其预期的方式对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!