0

我在互联网上找不到答案,所以我希望你能帮助我。我正在尝试从列表打印到 Tkinter 的文本框中。出于某种原因,当我将它打印到文本框时,它并没有按应有的方式对齐,但是当我将它打印到控制台时,它的对齐方式正确。

我使用的数据可以在data上找到。

你们中的任何人都知道可能是什么问题吗?

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

当前结果:

当前结果的屏幕截图

4

1 回答 1

6

问题:将表格格式的文本打印到 tk.Text 小部件中,未按预期对齐。
在控制台中,它正确对齐。

您必须使用固定宽度的字体,例如font=('Consolas', 10).

在此处输入图像描述


参考:


此示例显示了 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

于 2019-12-15T17:09:29.627 回答