i have an issue that i want to query from combo box value selected from sqllite database and show in graph,here is my code.I will be appreciate any one could help me.
from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
import pandas as pd
import sqlite3
from sqlalchemy import create_engine
from PyQt5 import QtCore, QtGui, QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
NavigationToolbar2Tk)
def imprt_data():
import_file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx")],
title = "Choose a file")
df = pd.read_excel(import_file_path)
return df
def svetosql():
df=imprt_data()
engine = create_engine('sqlite:///save_pandas.db', echo=True)
sqlite_connection = engine.connect()
sqlite_table="Data"
df.to_sql(sqlite_table, sqlite_connection, if_exists='fail')
def sqlconection():
con=sqlite3.connect('save_pandas.db')
c = con.cursor()
return c
def callback_obj(event):
comboobject=event.widget.get()
return (comboobject)
def callback_rgn(event):
comboregion=event.widget.get()
return (comboregion)
root=tk.Tk()
root.title('Testing Tkinter Combobox With Sqllite')
root.geometry('500x500')
##---------Widget---------------
comboregion=ttk.Combobox(root,width=50 , height=20)
c=sqlconection()
for row in c.execute('SELECT DISTINCT Data.??????? from data '):
Query = [row[0] for row in c.fetchall()]
comboregion['values']=Query
comboregion.pack( ipadx=2, ipady=2 )
selectvalueregion=comboregion.bind("<<ComboboxSelected>>", callback_rgn)
#------------------------
comboobject=ttk.Combobox(root,width=50 , height=20)
c=sqlconection()
for row in c.execute('SELECT DISTINCT from Data '):
Query = [member[0] for member in c.description]
comboobject['values']=Query
comboobject.pack( ipadx=2, ipady=2 )
selectvalueobject=comboobject.bind("<<ComboboxSelected>>", callback_obj)
for row in c.execute('SELECT DISTINCT * from Data WHERE region LIKE ? ;' , (selectvalueobject,)):
Q = [row[0] for row in c.fetchall()]
print(Q)
#------------------------
f = Figure(figsize=(5,5), dpi=80)
a = f.add_subplot(111)
a.plot([1,2,3,4,5,6,7,8],[5,6,1,3,8,9,3,5])
canvas = FigureCanvasTkAgg(f, root)
canvas.draw()
canvas.get_tk_widget().pack(ipadx=2, ipady=2)
root.mainloop()
the graph has some data only for show,but i want to get data from combo box and then make query from database and after that show in graph.
if any one want to get database ,please leave comments.
question from:
https://stackoverflow.com/questions/65874379/get-variable-of-combobox-for-sqllite-query-in-tkinter 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…