简体   繁体   English

将表格格式的文本打印到 tk.Text 小部件中,而不是按预期对齐

[英]Print tabular formated text into a tk.Text widget, not aligned as its supposed

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.我正在尝试从列表中打印到 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 .您可以在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()

Current result:当前结果:

当前结果的屏幕截图

Question : Print tabular formated text into a tk.Text widget, not aligned as its supposed.问题:将表格格式的文本打印到 tk.Text 小部件中,但未按预期对齐。
In the console, its aligned correctly.在控制台中,它正确对齐。

You have to use a fixed-width font, eg font=('Consolas', 10) .您必须使用固定宽度的字体,例如font=('Consolas', 10)

在此处输入图片说明


Reference:参考:


This example shows the usage in a tkinter Dialog :此示例显示了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

Usage :用法

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()

Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6用 Python 测试:3.5 - 'TclVersion':8.6 'TkVersion':8.6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM