Showing Data in MS SQL Database in TreeView - The Advantages of Learning Python Programming for Beginners: A Comprehensive Guide

Latest

Dive into the world of Python programming with our comprehensive resources

Music

logo

Wednesday, February 21, 2024

Showing Data in MS SQL Database in TreeView

 

In this article we will show the data in the MS Sql database in TreeView. First, we connect to the MS Sql database. Then we arrange the TreeView columns like below


Output:




 











from turtle import color

import pyodbc

from tkinter import ttk

 

import tkinter as tk

 

def connect():

       

    con1 = pyodbc.connect('Driver={SQL Server};'

                      'Server=sirius\SQLEXPRESS02;'

                      'Database=master;'

                      'Trusted_Connection=yes;')

    cur1 = con1.cursor()

 

 

    con1.commit()

 

    con1.close()

 

def View():

 

   

    con1 = pyodbc.connect('Driver={SQL Server};'

                      'Server=sirius\SQLEXPRESS02;'

                      'Database=master;'

                      'Trusted_Connection=yes;')

 

    cur1 = con1.cursor()

 

    cur1.execute("Select * From dbo.person")

 

    rows = cur1.fetchall()   

 

    for row in rows:

 

        print(row)

 

        tree.insert("", tk.END, values=row)       

 

    con1.close()

 

 

# connect to the database

 

connect()

 

root = tk.Tk()

 

root.title("msssql db example...bs")

 

root.geometry("750x500")

 

style = ttk.Style()

 

style.theme_use('clam')

 

style.configure("Treeview",background="lightyellow",foreground="#FFFFFF", font=('Arial', 12, 'normal'), rowheight=(20), fieldbackground="#FFFFFF")

style.map('Treeview', background=[('selected', '#FF0000')])

 

style.configure("Treeview.Heading",background="orangered",foreground="#FFFFFF", font = ('Arial', 14, 'bold'))

 

tree = ttk.Treeview(root, column=("c1", "c2", "c3","c4"), show='headings', height=18)

 

tree.column("#1", anchor=tk.CENTER,width=50)

 

tree.heading("#1", text="Id")

 

tree.column("#2", width=300)

 

tree.heading("#2", text="Name")

 

tree.column("#3",width=320)

 

tree.heading("#3", text="Surname")

 

tree.column("#4", anchor=tk.CENTER,width=50)

 

tree.heading("#4", text="Contact")

 

tree.pack()

 

button1 = tk.Button(text="Display data"+"\n"+"(Veri al)", command=View,width=20,height=2,font='arial 11 bold', fg='white', bg='deepskyblue')

 

button1.pack(pady=10)

 

root.mainloop()

 

 

Hope to see you in the next article. Happy coding

No comments:

Post a Comment

Featured post

Python Introduction

What is Python? Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built ...