Bottom Page

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

Quote
#2
Please be a bit more specific by referencing code lines.
Thank You.
Quote
#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
Quote
#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
Quote
#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.
Quote
#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)
Quote
#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'
Quote
#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)
Quote
#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)
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to store the value from variable into list & run a statement then put in variable searching1 0 51 Feb-16-2019, 12:35 AM
Last Post: searching1
  Trying to set an instance variable to current value of a class variable ScottDiesing 3 50 Feb-15-2019, 03:12 PM
Last Post: ScottDiesing
  Is it possible to create a variable with the name of an output of another variable Panda 8 660 Jun-06-2018, 09:14 PM
Last Post: Panda
  int(variable) not changing variable to an integer StoopidChicken 2 558 Jan-11-2018, 10:30 AM
Last Post: StoopidChicken
  Accept Multiple Lines of Input into a User Created File Bragger 0 645 Oct-29-2017, 04:15 PM
Last Post: Bragger
  Taking user input and storing that to a variable then storing that variable to a list jowalk 12 4,167 Mar-27-2017, 11:45 PM
Last Post: wavic
  Can PyAudio (Port Audio) validly accept float values when writing to stream? cdrandin 1 1,189 Mar-26-2017, 07:54 PM
Last Post: nilamo
  How do I accept input from the terminal when I execute my file? cocoa1231 1 1,193 Dec-08-2016, 11:22 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)