在Tkinter中创建表

时间:2019-07-19 08:34:17

标签: mysql python-3.x tkinter

我想使用MySQL中的select语句显示满足特定条件的所有数据,并将其以表格的形式与字段名称一起显示在Tkinter接口上。我的sql表包含字段DATE_OF_PROCUREMENT(date), PART_NO(varchar),COMPONENT_NAME(varchar),QUANTITY(big int),COST_PER_UNIT(float),TOTAL_COST(float)。有人可以帮忙吗?我已经尝试过:

import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkcalendar import Calendar, DateEntry

import MySQLdb as mydb
import datetime
import tkinter.messagebox
root=tk.Tk()
tabcontrol=ttk.Notebook(root)
tab1=ttk.Frame(tabcontrol)
tab2=ttk.Frame(tabcontrol)
tabcontrol.add(tab1,text="Entry Form")
tabcontrol.pack(expand=1,fill='both')
tabcontrol.add(tab2,text="Report")
tabcontrol.pack(expand=1,fill='both')
conn=mydb.connect(host='localhost',user='root',passwd='root',database='operator')
mycur=conn.cursor()
mycur.execute("USE OPERATOR")

def savedata():
    etext2=tk.Entry(tab1,textvariable=partno)
    etext3=tk.Entry(tab1,textvariable=cname)
    etext4=tk.Entry(tab1,textvariable=quantity)
    etext5=tk.Entry(tab1,textvariable=cpu)
    etext_2=etext2.get()
    etext_3=etext3.get()
    etext_4=etext4.get()
    etext_5=etext5.get()
    cal = DateEntry(width=12, background='darkblue',
                foreground='white', borderwidth=2)
    dt=cal.get_date()
    dt1=datetime.datetime.strftime(dt,'%Y-%m-%d')
    etext_6=int(etext_4)*float(etext_5)
    if float(etext_5) < 1000:
        s="INSERT INTO PROCUREMENT_FORM (DATE_OF_PROCUREMENT,PART_NO,COMPONENT_NAME,QUANTITY,COST_PER_UNIT,TOTAL_COST) VALUES (%s,%s,%s,%s,%s,%s)"
        ss=(dt1,etext_2,etext_3,etext_4,etext_5,etext_6)


mycur.execute(s,ss)

conn.commit()

def dateentry_view():
    def print_sel():
        global dt
        dt=cal.get_date()
        txt=tk.Label(tab1,text="%s"%dt)
        txt.place(x=400,y=50)

    top = tk.Toplevel(tab1)


    cal = DateEntry(top, width=12, background='darkblue',
                foreground='white', borderwidth=2)
    cal.place(x=80,y=50)
    ttk.Button(top, text="ok", command=print_sel).place(x=90,y=100)
root = tk.Tk()
s = ttk.Style(tab1)
s.theme_use('clam')
x=tk.IntVar()
partno=tk.StringVar()
cname=tk.StringVar()
quantity=tk.IntVar()
cpu=tk.DoubleVar()
ttk.Button(master=tab1, text='DateEntry', 
command=dateentry_view).place(x=190,y=50)


text2=tk.Label(tab1,text="Part No:",font=(28))
text3=tk.Label(tab1,text="Component name:",font=(28))
text4=tk.Label(tab1,text="Quantity:",font=(28))
text5=tk.Label(tab1,text="Cost per unit",font=(28))
text2.place(x=190,y=150)
text3.place(x=190,y=200)
text4.place(x=190,y=250)
text5.place(x=190,y=300)

etext2=tk.Entry(tab1,textvariable=partno)
etext3=tk.Entry(tab1,textvariable=cname)
etext4=tk.Entry(tab1,textvariable=quantity)
etext5=tk.Entry(tab1,textvariable=cpu)

etext2.place(x=400,y=150)
etext3.place(x=400,y=200)
etext4.place(x=400,y=250)
etext5.place(x=400,y=300)

etext_2=etext2.get()
etext_3=etext3.get()
etext_4=etext4.get()
etext_5=etext5.get()

def onClick(event=None):
    quantity.set(quantity.get() + 1)
def offClick(event=None):
    quantity.set(quantity.get() -1)

tk.Button(tab1, text="+", command=onClick, fg="dark green", bg = "white").place(x=260,y=230)
tk.Button(tab1,text='-',command=offClick,fg="dark green",bg="white").place(x=260,y=260)
tk.Button(tab1, text="Save",command=savedata).place(x=400,y=400)

root.withdraw()
pno=tk.StringVar()
def getfromdb():
    etext2=tk.Entry(tab1,textvariable=partno)
    etext3=tk.Entry(tab1,textvariable=cname)
    etext4=tk.Entry(tab1,textvariable=quantity)
    etext5=tk.Entry(tab1,textvariable=cpu)
    etext_2=etext2.get()
    etext_3=etext3.get()
    etext_4=etext4.get()
    etext_5=etext5.get()
    cal = DateEntry(width=12, background='darkblue',
                foreground='white', borderwidth=2)
    dt=cal.get_date()
    dt1=datetime.datetime.strftime(dt,'%Y-%m-%d')
    entry1=tk.Entry(tab2,textvariable=pno)
    entry1_=entry1.get()
    sel1="SELECT DATE_OF_PROCUREMENT,PART_NO,COMPONENT_NAME,QUANTITY,COST_PER_UNIT FROM PROCUREMENT_FORM WHERE PART_NO LIKE %s"
    mycur.execute(sel1,[entry1_])
    var1=[]
    var1.append(mycur.fetchall())
    sel2="SELECT COUNT(*) FROM PROCUREMENT_FORM WHERE PART_NO LIKE %s"
    mycur.execute(sel2,[entry1_])
    var2=mycur.fetchone()

    label1=var1



if(var2[0]>0):
    for i in range(0,var2[0]):
            for row in var1:
           #dt.set(row[0])
               tk.Label(text=label1,font=(28)).place(x=400+i,y=150+i)
               i=+20
    else:
        tk.messagebox.showinfo(message="Part number not found")
#Features for tab 2
tk.Label(tab2,text="Enter Part No ",font=(28)).place(x=100,y=100)
entry1=tk.Entry(tab2,textvariable=pno)
entry1.place(x=100,y=150)
entry1_=entry1.get()
w=tk.Button(tab2,text="Get Details",command=getfromdb)
w.place(x=120,y=170)
tab1.mainloop()

1 个答案:

答案 0 :(得分:1)

使用grid(row=..., column=...)代替place()可以创建表。 enumerate()还可用于获取(row=..., column=...)的值(x,y)

enter image description here

import tkinter as tk

# data from database

data = [
    ['A', 'B', 'C'],
    ['X', 'Y', 'Z'],
    [12345, 67890, 99999],
]

# --- main ---

root = tk.Tk()

for y, row in enumerate(data):
    for x, item in enumerate(row):
        l = tk.Label(root, text=str(item))
        l.grid(row=y, column=x)

root.mainloop()

如果您也将其放在带有黑色背景的Frame中,并在网格中使用填充,则可以得到

enter image description here

import tkinter as tk

# data from database

data = [
    ['A', 'B', 'C'],
    ['X', 'Y', 'Z'],
    [12345, 67890, 99999],
]

# --- main ---

root = tk.Tk()

frame = tk.Frame(root, bg='black')
frame.pack()

for y, row in enumerate(data):
    for x, item in enumerate(row):
        l = tk.Label(frame, text=str(item))
        l.grid(row=y, column=x, padx=1, pady=1, sticky='news')

root.mainloop()

但是当您只想显示数据时,这很有用。如果要突出显示行,按列排序,进行编辑,则可以TreeView