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

喜夏-厌秋 提交于 2019-12-25 01:12:13

问题


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

Current result:


回答1:


Question: Print tabular formated text into a tk.Text widget, not aligned as its supposed.
In the console, its aligned correctly.

You have to use a fixed-width font, e.g. font=('Consolas', 10).

  • A monospaced font, also called a fixed-pitch, fixed-width, or non-proportional font


Reference:

  • Tkinter.Text.config-method
  • Dialog Windows
  • tabulate output, displayed in a tk.Label, without to distort the data.

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

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



来源:https://stackoverflow.com/questions/59344319/print-tabular-formated-text-into-a-tk-text-widget-not-aligned-as-its-supposed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!