· 5 years ago · Mar 07, 2020, 05:42 PM
1from tkinter import *
2import tkinter.messagebox as tkMessageBox
3import sqlite3
4import tkinter.ttk as ttk
5import tkinter as tk
6from matplotlib.figure import Figure
7from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
8
9
10def Database():
11 global conn, cursor
12 conn = sqlite3.connect("pythontut.db")
13 cursor = conn.cursor()
14 cursor.execute(
15 "CREATE TABLE IF NOT EXISTS `admin` (admin_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT)")
16 cursor.execute(
17 "CREATE TABLE IF NOT EXISTS `product` (product_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, product_name TEXT, product_qty TEXT, product_price TEXT, product_sold TEXT, product_sellprice TEXT)")
18 cursor.execute("SELECT * FROM `admin` WHERE `username` = 'admin' AND `password` = 'admin'")
19 if cursor.fetchone() is None:
20 cursor.execute("INSERT INTO `admin` (username, password) VALUES('admin', 'admin')")
21 conn.commit()
22
23
24def Graphing():
25 class Root(Tk):
26 def __init__(self):
27 super(Root, self).__init__()
28 label_title = tk.Label(self, text="Graph Page", font=12)
29 label_title.pack(pady=10, padx=10)
30
31 #variables to use
32
33 tkvar = StringVar(self) #tkinter for x axis
34 tkvar1 = StringVar(self) #tkinter for yaxis
35 OPTIONS = ['product_qty','product_price','product_sold','product_sellprice']
36 OPTIONS1= ['product_qty','product_price','product_sold','product_sellprice']
37 tkvar.set(OPTIONS[1])
38 tkvar1.set(OPTIONS1[0])
39
40
41
42
43 #frames
44 right_frame = tk.Frame(self)
45 right_frame.pack(side=RIGHT)
46
47
48 y_label = tk.Label(right_frame, text="x = ")
49 y_label.grid(row=0, column=0)
50
51 x_label = tk.Label(right_frame, text="y = ")
52 x_label.grid(row=1, column=0)
53
54 #equation_box = tk.Entry(right_frame) # user equation entry
55 #equation_box.grid(row=0, column=1)
56
57 x = tk.OptionMenu(right_frame, tkvar, *OPTIONS)
58 x.grid(row=0, column=1)# option menu
59
60 y=tk.OptionMenu(right_frame, tkvar1, *OPTIONS1)
61 y.grid(row=1, column=1)
62
63 xaxis=tkvar.get()
64 yaxis=tkvar1.get()
65
66
67 draw_graph_button = tk.Button(right_frame, text="graph", height=2, width=15, bg="#b6bfcc",
68 command=lambda: plotgraph(xaxis,yaxis))
69 draw_graph_button.grid(row=2, column=1, pady=10)
70
71 fig=Figure() #blank canvas
72 a = fig.add_subplot(111) #adds the line
73
74
75
76 #print(xtoplot)
77 #print(ytoplot)
78
79
80
81 #equation_box = tk.Entry(self)
82 #equation_box.pack()
83 #self.minsize(640, 400)
84 #self.plotgraph()
85 #self.matplotCanvas()
86
87 #function that take in sql to plot
88 def plotgraph(xaxis,yaxis):
89 try:
90 a.clear()
91 #f = Figure(figsize=(5, 5), dpi=100)
92 Database()
93 mycursor = cursor.execute(f"SELECT {xaxis} FROM product")# select statement to get product_sold
94 x = mycursor.fetchall()
95 cursor.close()
96 conn.close() #finds xaxis
97
98 xtoplot = []
99 for t in x:
100 for x in t:
101 xtoplot.append(x)
102 xtoplot = list(map(float, xtoplot))
103
104 Database()
105 mycursor = cursor.execute(f"SELECT {yaxis} FROM product") # select statemnt for y axis
106 y = mycursor.fetchall()
107 cursor.close()
108 conn.close() #find y axis
109 ytoplot = []
110 for t in y:
111 for y in t:
112 ytoplot.append(y) # convert to list
113 ytoplot = list(map(float, ytoplot)) # convert to float
114 a.plot(xtoplot, ytoplot)
115 canvas.draw()
116 except:
117 print("Fuck off")
118
119
120 canvas = FigureCanvasTkAgg(fig, self)
121 canvas.draw()
122 canvas.get_tk_widget().pack(side=BOTTOM, fill=BOTH, expand=True)
123 toolbar = NavigationToolbar2Tk(canvas, self)
124 toolbar.update()
125 canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=True)
126
127 # a.plot([1, 2, 3, 4, 5, 6, 7, 8], [5, 6, 1, 3, 8, 9, 3, 5]) #data to plot
128
129
130 root = Root()
131 root.mainloop()
132
133'''
134OPTIONS = [
135"Jan",
136"Feb",
137"Mar"
138] #etc
139
140master = Tk()
141
142variable = StringVar(master)
143variable.set(OPTIONS[0]) # default value
144
145w = OptionMenu(master, variable, *OPTIONS)
146w.pack()
147
148def ok():
149 print ("value is:" + variable.get())
150
151button = Button(master, text="OK", command=ok)
152button.pack()
153
154mainloop()
155'''
156Database()
157Graphing()