我对Python很陌生,我开始尝试学习它来完成我在工作中获得的一个小项目。
基本上,我正在尝试复制MS访问数据库文件,从数据库中检索某些数据并使用Tkinter以网格格式显示它,然后删除复制的文件,因为这是一个重要的文件,因此该方法可以避免一些风险我的业余代码导致任何问题。
我已经设法使用pypyodbc连接到数据库,然后我为每列数据生成了一个查询,然后创建了一个网格小部件,以便在各自的列中显示这些数据。
我遇到的问题是,第0列结果中的行似乎占据了第1列结果的行,依此类推,其他列的结果也是如此,相应的数据是没有对齐。
到目前为止,这是我的代码,对它不是很好而道歉。
import pypyodbc
import shutil
import os
from tkinter import *
master = Tk()
master.title("Orders")
master.wm_iconbitmap('title icon.bmp')
Label(master, text="Order ID").grid(row=0, column=0, sticky=N)
Label(master, text="Customer").grid(row=0, column=1, sticky=N)
Label(master, text="Part No").grid(row=0, column=2, sticky=N)
Label(master, text="Ordered Quantity").grid(row=0, column=3, sticky=N)
Label(master, text="Shipped Quantity").grid(row=0, column=4, sticky=N)
Label(master, text="DateReqd").grid(row=0, column=5,sticky=N)
shutil.copy2('C:/Users/twaddington/Documents/Toms Raspberry Pi Project/MRP database copy/mrp4data.mdb', 'C:/Users/twaddington/Documents/Toms Raspberry Pi Project/MRP database copy/newmrp4data.mdb')
con = pypyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;UserCommitSync=Yes;Threads=3;SafeTransactions=0;PageTimeout=5;MaxScanRows=8;MaxBufferSize=2048;FIL={MS Access};DriverId=25;DefaultDir=C:/Users/twaddington/Documents/Toms Raspberry Pi Project/MRP database copy;DBQ=C:/Users/twaddington/Documents/Toms Raspberry Pi Project/MRP database copy/newmrp4data.mdb')
cursor = con.cursor()
cursor.execute("SELECT tblCustOrders.CustORID FROM tblStockItems INNER JOIN (tblSalesOrderDetail INNER JOIN (tblCustOrders INNER JOIN tblCusAddresses ON tblCustOrders.CustID = tblCusAddresses.AddID) ON tblSalesOrderDetail.SalesOrderID = tblCustOrders.CustORID) ON tblStockItems.ItemID = tblSalesOrderDetail.StockID WHERE (((tblSalesOrderDetail.OrderedQty)<>[ShippedQty]) AND ((tblCustOrders.ORComplete)=False));")
for CustORID in cursor.fetchall():
w = Label(master, text=CustORID).grid(column=0, sticky=W)
cursor.execute("SELECT tblCusAddresses.CoName FROM tblStockItems INNER JOIN (tblSalesOrderDetail INNER JOIN (tblCustOrders INNER JOIN tblCusAddresses ON tblCustOrders.CustID = tblCusAddresses.AddID) ON tblSalesOrderDetail.SalesOrderID = tblCustOrders.CustORID) ON tblStockItems.ItemID = tblSalesOrderDetail.StockID WHERE (((tblSalesOrderDetail.OrderedQty)<>[ShippedQty]) AND ((tblCustOrders.ORComplete)=False));")
for CoName in cursor.fetchall():
w = Label(master, text=CoName).grid(column=1, sticky=W)
cursor.execute("SELECT tblStockItems.MasterPNo FROM tblStockItems INNER JOIN (tblSalesOrderDetail INNER JOIN (tblCustOrders INNER JOIN tblCusAddresses ON tblCustOrders.CustID = tblCusAddresses.AddID) ON tblSalesOrderDetail.SalesOrderID = tblCustOrders.CustORID) ON tblStockItems.ItemID = tblSalesOrderDetail.StockID WHERE (((tblSalesOrderDetail.OrderedQty)<>[ShippedQty]) AND ((tblCustOrders.ORComplete)=False));")
for MasterPNo in cursor.fetchall():
w = Label(master, text=MasterPNo).grid(column=2, sticky=W)
cursor.execute("SELECT tblSalesOrderDetail.OrderedQty FROM tblStockItems INNER JOIN (tblSalesOrderDetail INNER JOIN (tblCustOrders INNER JOIN tblCusAddresses ON tblCustOrders.CustID = tblCusAddresses.AddID) ON tblSalesOrderDetail.SalesOrderID = tblCustOrders.CustORID) ON tblStockItems.ItemID = tblSalesOrderDetail.StockID WHERE (((tblSalesOrderDetail.OrderedQty)<>[ShippedQty]) AND ((tblCustOrders.ORComplete)=False));")
for OrderedQty in cursor.fetchall():
w = Label(master, text=OrderedQty).grid(column=3, sticky=W)
cursor.execute("SELECT tblSalesOrderDetail.ShippedQty FROM tblStockItems INNER JOIN (tblSalesOrderDetail INNER JOIN (tblCustOrders INNER JOIN tblCusAddresses ON tblCustOrders.CustID = tblCusAddresses.AddID) ON tblSalesOrderDetail.SalesOrderID = tblCustOrders.CustORID) ON tblStockItems.ItemID = tblSalesOrderDetail.StockID WHERE (((tblSalesOrderDetail.OrderedQty)<>[ShippedQty]) AND ((tblCustOrders.ORComplete)=False));")
for ShippedQty in cursor.fetchall():
w = Label(master, text=ShippedQty).grid(column=4, sticky=W)
cursor.execute("SELECT tblSalesOrderDetail.DateReqd FROM tblStockItems INNER JOIN (tblSalesOrderDetail INNER JOIN (tblCustOrders INNER JOIN tblCusAddresses ON tblCustOrders.CustID = tblCusAddresses.AddID) ON tblSalesOrderDetail.SalesOrderID = tblCustOrders.CustORID) ON tblStockItems.ItemID = tblSalesOrderDetail.StockID WHERE (((tblSalesOrderDetail.OrderedQty)<>[ShippedQty]) AND ((tblCustOrders.ORComplete)=False));")
for DateReqd in cursor.fetchall():
w = Label(master, text=DateReqd).grid(column=5, sticky=W)
cursor.close()
con.close()
os.remove('C:/Users/twaddington/Documents/Toms Raspberry Pi Project/MRP database copy/newmrp4data.mdb')
master.mainloop()
And here is a screenshot of the issue I am facing.
这个问题一直困扰我一个星期,我似乎无法找到解决方案。
我正在使用Python 3.6
非常感谢任何大小帮助。
谢谢。
答案 0 :(得分:0)
这样的事情会有所作为吗?
for x, CustORID in enumerate(cursor.fetchall()):
w = Label(master, text=CustORID).grid(column=0, row=x+1, sticky=W)