我正在使用 tkinter 在 python 中創建一個 Inventory 專案,我想要它做的一件事是顯示存盤在文本檔案中的整個庫存。到目前為止,我已經讓它作業了,但是在讀取到框架后的文本格式沒有對齊并且看起來不太好。

def openFile():
tf = filedialog.askopenfilename(
initialdir="C:/Users/MainFrame/Desktop/",
title="Open Text file",
filetypes=(("Text Files", "*.txt"),)
)
tf = open(tf) # or tf = open(tf, 'r')
data = tf.read()
self.txtInventory.insert(END, data)
tf.close()
if __name__=='__main__':
root=Tk()
application= Inventory(root)
root.mainloop()
uj5u.com熱心網友回復:
通常我會使用標簽進行格式化:
from tkinter import *
root = Tk()
root.geometry('400x250 800 50')
txtInventory = Text(root, wrap='word', padx=10, pady=10)
txtInventory.pack(fill='both', padx=10, pady=10)
txtInventory.tag_config('fixed', font='TkFixedFont') # Fixed style
# Tag name ----^
txtInventory.tag_add('fixed', '1.0', 'end') # Apply to entire text
# Insert example text
contents = '''ItemName Qty Bay#
Bleach 17 4
Towels 20 3'''
txtInventory.insert('end', contents)
root.mainloop()
看看Tkinter 文本樣式演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/456392.html
下一篇:GetX也不接受方法引數
