Python Forum
Pyodbc does not accept variable from tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pyodbc does not accept variable from tkinter
#1
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()
Reply
#2
Please be a bit more specific by referencing code lines.
Thank You.
Reply
#3
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
Reply
#4
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
Reply
#5
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.
Reply
#6
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)
Reply
#7
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'
Reply
#8
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)
Reply
#9
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)
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using pyodbc&pandas to load a Table data to df tester_V 3 746 Sep-09-2023, 08:55 PM
Last Post: tester_V
  accept command line argument mg24 5 1,239 Sep-27-2022, 05:58 PM
Last Post: snippsat
  pyodbc gmerritt 8 2,798 Feb-21-2022, 07:21 PM
Last Post: gmerritt
  Formatting Data/Time with Pyodbc and openpyxl bearcats6001 0 2,251 Aug-17-2020, 03:44 PM
Last Post: bearcats6001
  Get database used data space from pyodbc susja 1 2,201 Aug-14-2020, 02:01 PM
Last Post: susja
  I need my compiled Python Mac app to accept a file as a parameter Oethen 2 2,357 May-10-2020, 05:57 PM
Last Post: Oethen
  pyodbc error ('82', '[82] 523 80 (0) (SQLDriverConnect)') paulsuk1982 1 2,133 Nov-29-2019, 11:05 AM
Last Post: Larz60+
  pyodbc.Error SQLBindParameter pcarra 0 3,841 Jul-08-2019, 08:22 PM
Last Post: pcarra
  Pyodbc error taxit 1 5,139 Jun-18-2019, 01:13 AM
Last Post: Larz60+
  Using VBA to Call a Python script causes error in pyodbc connector pcarra 1 2,775 Jun-11-2019, 04:14 PM
Last Post: pcarra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020