[Python] Problem Displaying MS Sql Query in python

Discussão em 'Python' iniciado por Stack, Agosto 20, 2025.

  1. Stack

    Stack Membro Participativo

    Hello I am Trying to Display a Simple Query from my Database in python tkinter my problem is the data wont sit in the correct form (Mostly Because of extra spaces) how Can i Remove Spaces in a Query stored in a list my code :

    # Importing Required to Create GUI
    import pyodbc
    #Import tkinter but as tk (Shortetning the name that we use to call it)
    import tkinter as tk
    from tkinter import *
    from tkinter import ttk
    from tkinter.messagebox import showinfo
    #Connect to DataBase
    connection = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
    'Server=POORIA-PC;'
    'Database=BooksDataBase;'
    'Trusted_Connection=yes;')
    #Define our Cursor
    cursor = connection.cursor()
    # Define Main Window

    Mainwin = tk.Tk()

    #Title for Main Window(ROOT)
    Mainwin.title('Hello Dear User ^_^')

    #Define Main Window Height and Width

    Main_Window_Width = 800
    Main_Window_Height = 600

    #Return the Screens current dimensions (MONITOR DIMENSIONS)

    Screen_Width = Mainwin.winfo_screenwidth()
    Screen_Height = Mainwin.winfo_screenheight()

    #Calculate the Center of the Screen

    Center_X = int(Screen_Width/2 - Main_Window_Width/2)
    Center_Y = int(Screen_Height/2 - Main_Window_Height/2)
    #NOW DEFINE THE GEOMETRY OF THE MAIN WINDOW BY PASSING IT THE ABOVE VARIABLES

    Mainwin.geometry(f'{Main_Window_Width}x{Main_Window_Height}+{Center_X}+{Center_Y}')
    #Lock the Dimensions so user cant change it
    Mainwin.resizable(False , False)

    #Define Transparency of the Main Window
    Mainwin.attributes('-alpha',1.0)
    Mainwin.iconbitmap('./Icons/MainWinIcon.ico')
    #Retrieve a List from SQL Database
    Data = cursor.execute("SELECT * FROM BooksMain")
    for i in Data:
    print(i)

    # define columns
    columns = ('UID', 'Name', 'Category', 'QTY', 'Price')

    tree = ttk.Treeview(Mainwin, columns=columns, show='headings')

    # define headings
    tree.heading('UID', text='UID')
    tree.heading('Name', text='Name')
    tree.heading('Category', text='Category')
    tree.heading('QTY', text='QTY')
    tree.heading('Price', text='Price')

    for j in Data:
    tree.insert('', tk.END, values=Data)


    def item_selected(event):
    for selected_item in tree.selection():
    item = tree.item(selected_item)
    record = item['values']
    # show a message
    showinfo(title='Information', message=','.join(record))


    tree.bind('<<TreeviewSelect>>', item_selected)

    tree.grid(row=0, column=0, sticky='nsew')

    # add a scrollbar
    scrollbar = ttk.Scrollbar(Mainwin, orient=tk.VERTICAL, command=tree.yview)
    tree.configure(yscroll=scrollbar.set)
    scrollbar.grid(row=0, column=1, sticky='ns')


    #Call the Main Window
    Mainwin.mainloop()


    I Created a Treeview and the Result was Names in wrong Columns because of the spaces so now im trying to print it to see if i can stored without any extra spaces

    (1, 'Harry Potter and the Goblet of Fire', 'Magic', 'J. K. Rowling', 2, '5000')
    (2, 'Harry potter and the goblet of fire', 'Magic', 'J. K. Rowling', 10, '10000')
    (3, 'Harry potter and the order of the phoenix', 'Magic', 'J. K. Rowling', 1, '5000')
    (4, 'Harry potter and the Deathly Hallows Part I', 'Magic', 'J. K. Rowling', 1, '5000')
    (5, 'Lord of the Rings', 'Magic', 'J. K. Rowling', 85, '20000')


    this is the result I get after a query

    Continue reading...

Compartilhe esta Página