我在互联网上找不到答案,所以希望您能为我提供帮助。我正在尝试从列表打印到Tkinter的文本框中。由于某种原因,当我将其打印到文本框中时,它并未按预期对齐,但在我将其打印到控制台时,其正确对齐了。
我正在使用的数据可以在data上找到。
你们中的任何人都知道可能是什么问题吗?
Button(
action: {
UIApplication.shared.open(self.url!)
}
){
Text("Tap here.")
}
当前结果:
答案 0 :(得分:4)
问题:将表格格式的文本打印到tk.Text小部件中,但未按预期对齐。
在控制台中,其正确对齐。
您必须使用固定宽度的字体,例如font=('Consolas', 10)
。
参考:
tk.Label
, without to distort the data. 此示例显示了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