Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Pyodbc does not accept variable from tkinter
Hi guys, I am a beginner however I achieved a good result taking information from my servers and passing to a tkinter form. Now I ma trying to choosing dynamically my servers but I am messing up with Pyodbc and Tkinter in order to take a variable and to pass it to the pyodbc.connect.
Any advice?

import pandas
from tkinter import *
from tkinter import ttk
import pyodbc
from tkinter import messagebox

master = Tk()

#var = StringVar(master)
#var.set("Choose Server") # default value
#tenda = OptionMenu(master, var, "Server=CGL-SC01-DSTAGE\DSTAGE", "Server=CGL-SC01-DWAREH\DWAREH", "three")

s = StringVar()
om = OptionMenu(master, s, 'a', 'b', 'c', 'd')
def changed(*args):

s.trace('w', changed)

w = Label(master, text="Database Tables Size Control", width=800, height=4, bg="Yellow", font=("Courier", 14, "bold"))
w2 = Label(master, text="", width=800, height=1, bg="White")
tree = ttk.Treeview(master)
tree.column("one", width=100)
tree.column("two", width=100)
tree.column("three", width=100 )
tree.column("four", width=100)
tree.column("five", width=100 )
tree.column("six", width=100 )
tree.heading("one", text="DataBaseName")
tree.heading("two", text="FileGroup")
tree.heading("three", text="File Size")
tree.heading("four", text="Used Space")
tree.heading("five", text="Free Space")
tree.heading("six", text="Free Space %")

cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
sql = """
Exec ReadSize

df =, cnxn)[ dict(selector='th', props=[('text-align', 'right')] ) ])
for index, row in df.iterrows():
    if row["FreeSpace %"] > 5:
       tree.insert("", 0, text="Normal", values=(row["DatabaseName"],row["FileGroupName"],row["FileSize"], row["UsedSpace"],row["FreeSpace"], row["FreeSpace %"]), tags=('oddrow',))
       tree.insert("", 0, text="Warning", values=(row["DatabaseName"], row["FileGroupName"], row["FileSize"], row["UsedSpace"], row["FreeSpace"], row["FreeSpace %"]),tags=('evenrow',))

    tree.pack(expand=True, fill='y')


tree.tag_configure('oddrow', background='white')
tree.tag_configure('evenrow', background='#fe8b62')
#tree.insert("", 0,    text="Line 1", values=(df))

def callback():
    df.to_csv('//My Documents/TestPython/Prova.csv')
    messagebox.showinfo("Information", "CSV Downloaded")

b = Button(master, text="CSV", command=callback)


Please be a bit more specific by referencing code lines.
Thank You.
Sure, sorry, totally beginner. I've got the OptionMenu:
s = StringVar()
om = OptionMenu(master, s, 'Server1', 'Server2', 'Server3', 'd')
def changed(*args):

I'd like ON CHANGE to pass the value to the pyodbc connection:


cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
instead to use Datab as variable I'd like to use changed (from option) and to refresh the mask after changing the value in the option menu.

I hope I'been able to explain my request.
The docs tell you how to get the option chosen from an OptionMenu. For future reference don't throw some code against a forum wall that you think looks good, and hope someone writes the program for you. Also, don't ask a question that is in every tutorial as no one will help you from there on
Hi Wooee,
As I pointed out I am an absolute beginner. I help a lot around in the forum where I am an expert and when I see a beginner I like helping him/her without throwing stupid comments. If you don't want helping it is ok, but please ignore my post starting from now. I don't need people like you.
Untested but should work:
def OptionMenu_SelectionEvent(event):
    print("do whatever here')

optionList = ('Server1', 'Server2', 'Server3', 'd')
    s = tk.StringVar()
    s.set('a') = tk.OptionMenu(master, s, *optionList, command = OptionMenu_SelectionEvent)
Thanks a lot, Larz60+. Unfortunately, I retrieve an error:

optionList = ("Server1;", "Server2;")
s = Tk.StringVar()
s.set('a') = Tk.OptionMenu(master, s, *optionList, command=OptionMenu_SeletionEvent())
type object 'Tk' has no attribute 'StringVar'
you didn't show enough code for me to answer properly.
What i show will work if tkinter is imported like:
import tlinter as tk
if you used:
 import tkinter
then it would be:
s = tkinter.StringVar()
looking at what you do provide (This is why it's important to include enough code to run an example)
I guess it would be:
def OptionMenu_SelectionEvent(event):
    print("do whatever here')
optionList = ('Server1', 'Server2', 'Server3', 'd')
    s = StringVar()
    s.set('a') = OptionMenu(master, s, *optionList, command = OptionMenu_SelectionEvent)
Just writing only this code I retrieve errors...hard to understand why. But thanks Larsz+60

import tkinter as tk

master = Tk()

def Option_SelectionEvent(event):
    print("do it")

optionList = ("Server1","Server2","Server3")
    s = StringVar()
    s.set("a") = OptionMenu(master, s, *optionList, command=Option_SelectionEvent)
you need to use prefix tk
def Option_SelectionEvent(event):
    print("do it")
optionList = ("Server1","Server2","Server3")
    s = tk.StringVar()
    s.set("a") = tk.OptionMenu(master, s, *optionList, command=Option_SelectionEvent)
it all depends on how you import the package
when you write:
import tkinter as tk
your saying import package tkinter, and for my program name tkinter tk
so therefore anywhere tkinter would normally be needed, you use tk instead.

if you import thusly:
from tkinter import *
no prefix is needed, so:
s = StringVar()

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  I need my compiled Python Mac app to accept a file as a parameter Oethen 2 287 May-10-2020, 05:57 PM
Last Post: Oethen
  pyodbc error ('82', '[82] 523 80 (0) (SQLDriverConnect)') paulsuk1982 1 354 Nov-29-2019, 11:05 AM
Last Post: Larz60+
  pyodbc.Error SQLBindParameter pcarra 0 589 Jul-08-2019, 08:22 PM
Last Post: pcarra
  Pyodbc error taxit 1 1,648 Jun-18-2019, 01:13 AM
Last Post: Larz60+
  Using VBA to Call a Python script causes error in pyodbc connector pcarra 1 709 Jun-11-2019, 04:14 PM
Last Post: pcarra
  Split List and Sublist from Pyodbc parthi1705 1 546 May-05-2019, 10:44 AM
Last Post: Larz60+
  Problem with pyodbc executemany() RBeck22 1 2,510 Apr-02-2019, 06:12 PM
Last Post: micseydel
  PyODBC error - second parameter to executemany must be a sequence, iterator, or gener RBeck22 1 1,833 Mar-29-2019, 06:44 PM
Last Post: RBeck22
  What is the correct type hint when you want to accept Iterable but not Dictionary LadySvetlana 4 681 Mar-05-2019, 07:33 PM
Last Post: LadySvetlana
  Accept Multiple Lines of Input into a User Created File Bragger 0 1,168 Oct-29-2017, 04:15 PM
Last Post: Bragger

Forum Jump:

Users browsing this thread: 1 Guest(s)