Skip to content Skip to sidebar Skip to footer

Tkinter Mysql - How To Get Different Information While Using Entries Whith The 'fetch' Method?

I have in my appliction an entry to enter 'mysql-code' and search information from database, which is showing the typed text bye execute the entry in my treeviews. SELECT * FROM wo

Solution 1:

Below is an example to show the result of SQL query using Treeview which is configured based on the result dynamically.

import tkinter as tk
from tkinter import ttk
import mysql.connector

# use your database configuration
db = mysql.connector.connect(host="localhost", user="user", password="password", database="database")

def execute_sql(event):
    sql = sqlbox.get("1.0", "end-1c")
    cursor = db.cursor()
    cursor.execute(sql)
    # configure treeview
    table.config(columns=cursor.column_names)
    for col in cursor.column_names:
        table.heading(col, text=col)
    # insert data
    table.delete(*table.get_children())
    for rec in cursor:
        table.insert("", "end", values=rec)
    cursor.close()

root = tk.Tk()
root.geometry("1000x800")

# text box to input SQL statement
sqlbox = tk.Text(root, height=10)
sqlbox.pack(fill="x")
sqlbox.bind("<F9>", execute_sql)

# treeview to show the query result
frame = ttk.Frame(root)
frame.pack(fill="both", expand=1)

frame.rowconfigure(0, weight=1)
frame.columnconfigure(0, weight=1)

table = ttk.Treeview(frame, show="headings")
table.grid(row=0, column=0, sticky="nsew")

vsb = ttk.Scrollbar(frame, orient="vertical", command=table.yview)
vsb.grid(row=0, column=1, sticky="ns")

hsb = ttk.Scrollbar(frame, orient="horizontal", command=table.xview)
hsb.grid(row=1, column=0, sticky="ew")

table.config(xscrollcommand=hsb.set, yscrollcommand=vsb.set)

root.mainloop()

Post a Comment for "Tkinter Mysql - How To Get Different Information While Using Entries Whith The 'fetch' Method?"