Python Forum
Pyodbc does not accept variable from tkinter - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: General Coding Help (https://python-forum.io/Forum-General-Coding-Help)
+--- Thread: Pyodbc does not accept variable from tkinter (/Thread-Pyodbc-does-not-accept-variable-from-tkinter)



Pyodbc does not accept variable from tkinter - diegoctn - Feb-08-2019

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()




RE: Pyodbc does not accept variable from tkinter - Larz60+ - Feb-08-2019

Please be a bit more specific by referencing code lines.
Thank You.


RE: Pyodbc does not accept variable from tkinter - diegoctn - Feb-08-2019

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


RE: Pyodbc does not accept variable from tkinter - woooee - Feb-08-2019

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


RE: Pyodbc does not accept variable from tkinter - diegoctn - Feb-11-2019

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.


RE: Pyodbc does not accept variable from tkinter - Larz60+ - Feb-11-2019

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)



RE: Pyodbc does not accept variable from tkinter - diegoctn - Feb-12-2019

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'


RE: Pyodbc does not accept variable from tkinter - Larz60+ - Feb-12-2019

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)



RE: Pyodbc does not accept variable from tkinter - diegoctn - Feb-12-2019

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)



RE: Pyodbc does not accept variable from tkinter - Larz60+ - Feb-12-2019

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.