Python Forum

Full Version: Pyodbc does not accept variable from tkinter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
master.geometry("800x800")
master.configure(background='#3c485f')

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


s = StringVar()
s.set('a')
om = OptionMenu(master, s, 'a', 'b', 'c', 'd')
om.pack()
def changed(*args):
    print(s.get())

s.trace('w', changed)

w = Label(master, text="Database Tables Size Control", width=800, height=4, bg="Yellow", font=("Courier", 14, "bold"))
w.pack()
w2 = Label(master, text="", width=800, height=1, bg="White")
w2.pack()
tree = ttk.Treeview(master)
tree["columns"]=("one","two","three","four","five","six")
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};"
                      changed()";"
                      "Database=master;"
                      "Trusted_Connection=yes;")
sql = """
Exec ReadSize
"""

df = pandas.io.sql.read_sql(sql, cnxn)
df.style.set_table_styles([ dict(selector='th', props=[('text-align', 'right')] ) ])
df.head(0)
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',))
    else:
       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')


print(df)



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

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


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


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

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

Datab="Server1;"
print(Datab)

cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                      +str(Datab)+
                      "Database=master;"
                      "Trusted_Connection=yes;")
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.
Thanks
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 http://effbot.org/tkinterbook/optionmenu.htm
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')
    self.om = 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')
self.om = 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')
    self.om = 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()
master.geometry("800x800")
master.configure(background='#3c485f')



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

optionList = ("Server1","Server2","Server3")
    s = StringVar()
    s.set("a")
    self.om = 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")
    self.om = 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()
works.