我是编码的新手,所以请原谅我任何简单的疏忽 我正在编写一个程序来查询我的本地SQL数据库并返回有关数据的基本统计信息。我正在尝试构建一个简单的tkinter GUI,它将控制两个变量因子并在文本框中打印结果 我目前只是从输入框中获取变量。
我使用的是Python 3.6,我的代码如下。
非常感谢任何帮助。
from tkinter import *
from tkinter.ttk import *
import pandas as pd
import sqlalchemy as sc
engine = sc.create_engine("mssql+pyodbc://DC2:password@SQLconn")
class SimLvGUI(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.init_ui()
def init_ui(self):
self.parent.title("SimLv")
self.pack(fill=BOTH, expand=1)
lbl = Label(self, text="Enter FTR Source Sink")
lbl.grid(row=0, column=0, sticky=N + W, pady=4, padx=5)
entry1 = Entry(self)
entry1.grid(row=1, column=0, columnspan=1, padx=5, sticky=N + W)
lb2 = Label(self, text="Enter FTR Month as Number")
lb2.grid(row=2, column=0, sticky=N + W, pady=4, padx=5)
entry2 = Entry(self)
entry2.grid(row=3, column=0, columnspan=1, padx=5, sticky=N + W)
run_button = Button(self, text="Run")
run_button.grid(row=4, column=0, pady=4, padx=5, sticky=N + W)
run_button.config(command=self.on_button)
lb3 = Label(self, text="Historic spot FTR stats")
lb3.grid(row=5, column=0, padx=5, sticky=N + W)
results1 = Text(self, width=30)
results1.grid(row=6, column=0, rowspan=10, padx=5, sticky=W + N)
lb4 = Label(self, text="Historic FTR clearing stats")
lb4.grid(row=5, column=1, padx=5, sticky=N + W)
results2 = Text(self, width=30)
results2.grid(row=6, column=1, rowspan=10, padx=5, sticky=W + N)
lb5 = Label(self, text="Last FTR clearing price")
lb5.grid(row=5, column=2, padx=5, sticky=N + W)
results3 = Text(self, width=30, height=10)
results3.grid(row=6, column=2, rowspan=5, padx=5, sticky=W + N)
lb6 = Label(self, text="High volume FTR clearing price")
lb6.grid(row=11, column=2, padx=5, sticky=N + W)
results3 = Text(self, width=30, height=10)
results3.grid(row=12, column=2, rowspan=5, padx=5, sticky=W + N)
def on_button(self):
ftr_source_sink = self.entry1.get()
ftr_month = self.entry2.get()
def run_simlv(self):
sql1 = ("SELECT Avg({0}) as {0} "
"FROM dbo.FTR_matrix "
"WHERE month(Trading_date) = {1} "
"GROUP BY month(Trading_date), year(Trading_date) ORDER BY
year(Trading_date), month(Trading_date) "
.format(ftr_source_sink, ftr_month))
# Import FTR clearing data from SQL server.
sql2 = ("SELECT Price "
"FROM dbo.FTR_Reg "
"WHERE FTR = '{0}' and Mth = {1} and HedgeType = 'OPT' and MW>0 and
Price>0 "
"ORDER BY Mth, Yr ".format(ftr_source_sink, ftr_month))
# Return most recent FTR clearing price.
sql3 = ("SELECT DateAcquired, Mth, Yr, FTR, MW, Price "
"FROM (SELECT m.*, row_number() over (partition by FTR order by
DateAcquired desc) as rn "
"FROM dbo.FTR_Reg m "
"WHERE FTR = '{0}' and Mth = {1} and HedgeType = 'OPT' ) m2 "
"WHERE m2.rn = 1 ".format(ftr_source_sink, ftr_month))
# Return the highest volume FTR purchase
sql4 = ("SELECT DateAcquired, Mth, Yr, FTR, MW, Price "
"FROM (SELECT m.*, row_number() over (partition by FTR order by MW
desc) as rn "
"FROM dbo.FTR_Reg m "
"WHERE FTR = '{0}' and Mth = {1} and HedgeType = 'OPT' ) m2 "
"WHERE m2.rn `enter code here`= 1 ".format(ftr_source_sink, enter
code here ftr_month))
# Output historic spot statistics
print('Spot Price Statistics')
ftr_month_value = pd.read_sql_query(sql1, engine)
print(ftr_month_value.describe())
# Output historic clearing statistics
print('Clearing Price Statistics')
ftr_clearing = pd.read_sql_query(sql2, engine)
print(ftr_clearing.describe())
# Output last clearing price
print('Last Clearing Price')
ftr_lastprice = pd.read_sql_query(sql3, engine)
print(ftr_lastprice)
# Output highest volume cleared
print('Clearing Price at highest cleared volume')
ftr_high_volume = pd.read_sql_query(sql4, engine)
print(ftr_high_volume)
def main():
root = Tk()
root.geometry("900x600+200+50")
app = SimLvGUI(root)
root.mainloop()
if __name__ == '__main__':
main()
答案 0 :(得分:0)
您的on_button
没有任何实际意义,因为ftr_source_sink
和ftr_month
在您的函数定义中存在且死。尝试将它们保存为属性:
def on_button(self):
self.ftr_source_sink = self.entry1.get()
self.ftr_month = self.entry2.get()
以便您以后可以在类定义中的任何位置访问它们。
作为旁注,如果您决定采用这种方法,通常最好在__init__
内初始化这些属性:
def __init__(self, parent):
# your code
self.ftr_source_sink = None
self.ftr_month = None