I want to output the loaded file as a table in Tkinter.

Asked 1 years ago, Updated 1 years ago, 438 views

I would like to print the prepared text file in tabular format using tkinter.It is assumed that the next column will be printed after checking the blank space, and the next column will be printed after checking the new line.However, I don't know how to create a program that will result.

error messages:

Traceback (most recent call last):
  File "C:/Program Files/Python36/Topology Information Output .py", line 29, in <module>
    tree.insert(parent=', index=0, iid=0, values=(i,line))
  File "C:\Program Files\Python36\lib\tkinter\ttk.py", line 1341, insert
    "-id", iid, *opts)
_tkinter.TclError: Item 0 already exists

Current State Code:

import tkinter ask
from tkinter import ttk
from tkinter import Frame
from tkinter import Button
from tkinter import LEFT
import tkinter.filedialog as fd

# Invoking a File
path=fd.askopenfilename()

# Generating the Main Window
root=tk.Tk()
root.title ("Output in Table")

frame = Frame(root)

# Treeview Generation
tree=ttk.Treeview(frame, columns=(1,2), show='headings', height=20)

# Column Headline Settings
tree.heading(1, text="1")
tree.heading(2, text="2")

i = 0
# Loading a Text File
with open(path, "r") as f:
    for line inf:
        i=i+1
        tree.insert(parent=', index=0, iid=0, values=(i,line))
run=int(len(line)/2)

# Widget Placement
frame.pack()
tree.pack (side=LEFT)
scrollbar.pack (side=tk.RIGHT, fill=tk.Y)
button.pack()

tk.mainloop()

python python3 tkinter

2022-12-15 03:50

1 Answers

The How to create a table (Treeview) will help you create a table

import tkinter ask
from tkinter import ttk
from tkinter import Frame
from tkinter import Button
from tkinter import LEFT
import tkinter.filedialog as fd

# Invoking a File
path=fd.askopenfilename()

# Generating the Main Window
root=tk.Tk()
root.title ("Output in Table")

frame = Frame(root)

# Treeview Generation
tree=ttk.Treeview(frame, columns=(1,2), show='headings', height=20)

# Column Headline Settings
tree.heading(1, text="1")
tree.heading(2, text="2")

i = 0
# Loading a Text File
with open(path, "r") as f:
    for line inf:
        tree.insert(parent=', index=i, iid=i, values=(line.split()))###Modify
        i=i+1
run=int(len(line)/2)

# Widget Placement
frame.pack()
tree.pack (side=LEFT)
scrollbar=tk.Scrollbar(frame, orient=tk.VERTICAL, command=tree.yview)## Added
scrollbar.pack (side=tk.RIGHT, fill=tk.Y)
# button.pack()

tk.mainloop()

<scrollbar has NameError, so I added the code here.
button has a NameError and is commented out.


2022-12-15 08:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.