import tkinter as tk
from tkinter import ttk
import xml.etree.ElementTree as ET
# 1. Sample XML Data (could be loaded from a file)
xml_data = """<?xml version="1.0"?>
<library>
<book id="1">
<title>Python Basics</title>
<author>John Doe</author>
<year>2020</year>
</book>
<book id="2">
<title>Tkinter Guide</title>
<author>Jane Smith</author>
<year>2022</year>
</book>
</library>
"""
def load_xml_data(tree):
# Parse XML
root = ET.fromstring(xml_data)
# Iterate over XML elements and insert into treeview
for book in root.findall('book'):
book_id = book.get('id')
title = book.find('title').text
author = book.find('author').text
year = book.find('year').text
tree.insert("", "end", values=(book_id, title, author, year))
# Create GUI
root = tk.Tk()
root.title("XML to Treeview")
# 2. Setup Treeview
columns = ("id", "title", "author", "year")
tree = ttk.Treeview(root, columns=columns, show="headings")
# Define Headings
tree.heading("id", text="ID")
tree.heading("title", text="Title")
tree.heading("author", text="Author")
tree.heading("year", text="Year")
# Define Columns
tree.column("id", width=50)
tree.column("title", width=200)
tree.column("author", width=150)
tree.column("year", width=50)
tree.pack(pady=20)
# Load data
load_xml_data(tree)
root.mainloop()
Tidak ada komentar:
Posting Komentar