根据用户输入更改sql数据库中的数量?

问题描述 投票:0回答:1

我必须遵循以下脚本来运行树视图脚本,才能显示数据库中包含的信息:

from tkinter import *
from tkinter import ttk
import sqlite3
from tkinter.ttk import *
import os

root = Tk()
root.state('zoomed')
root.title("Inventory Balance")
conn = sqlite3.connect('MyStock.sql3')


def db():
    global conn, mycursor
    mycursor = conn.cursor()


def data():
    tree.delete(*tree.get_children())
    mycursor.execute("SELECT * FROM MyStock")  # Set mycursor as row to execute in. Read database
    for row in mycursor:
        tree.insert('', 'end', values=row[0:6])
    conn.close()

frame = Frame(root)
frame.pack()

style = ttk.Style()
style.configure("Treeview.Heading", font=("Arial", 14), foreground="yellow", background="black")

tree = ttk.Treeview(frame, columns=(0, 1, 2, 3, 4), height=30, show="headings")
tree.pack(side='left')

tree.heading(0, text="Item Code")
tree.heading(1, text="Description")
tree.heading(2, text="Category")
tree.heading(3, text="Unit")
tree.heading(4, text="Quantity")

tree.column(0, width=300)
tree.column(1, width=300)
tree.column(2, width=300)
tree.column(3, width=150)
tree.column(4, width=150)

# Inserting Scrollbar
scroll = ttk.Scrollbar(frame, orient="vertical", command=tree.yview)
scroll.pack(side='right', fill='y')

tree.configure(yscrollcommand=scroll.set)

# Configuring style
style = Style()
style.configure('W.TButton', font=('calibri', 10, 'bold', 'underline'), foreground='red')

# Program to open with Button1


def userform1():
    os.system('python UserForm_Test_altered_.py')

# Inserting Buttons
''' Button 1'''
btn1 = Button(root, text='Exit', style='W.TButton', command=root.destroy)
btn1.pack(side='right')

''' Button 2'''
btn2 = Button(root, text='Issue / Receive', command=userform1)
btn2.pack(side='left')

db()
data()
root.mainloop()

通过提交按钮,我打开一个GUI,如下所示:Userform_Test_altered_.py

带有代码:

from tkinter import *
import sqlite3
from tkinter.ttk import *
import os

root = Tk()
root.geometry('380x400')
# root.state('zoomed')
root.title("Received")
style = Style()

ItemCode = StringVar()
Description = StringVar()
Quantity = IntVar()
Unit = StringVar()
var = IntVar()
c = StringVar()
var1 = IntVar()
var2 = IntVar()


def database():
    code = ItemCode.get()

    # desc = Description.get()

    quantity = Quantity.get()

    # unit = Unit.get()

    # dep = c.get()

    conn = sqlite3.connect('MyStock.sql3')

    with conn:
        cursor = conn.cursor()
        # sql = 'UPDATE MyStock SET Quantity=?, Category=?, Unit=?, Description=? WHERE ItemCode=?'
        # cursor.execute(sql, (quantity, dep, unit, desc, code))
        cursor.execute('UPDATE MyStock SET Quantity=? WHERE ItemCode=?', (quantity, code,))
        cursor.close()
        conn.commit()


def data2():
    os.system('python main_screen.py')

# Inserting labels and field of input
label_0 = Label(root, text="Stock Movement", width=20, font=("bold", 20)).place(x=90, y=23)

label_1 = Label(root, text="Item Code", width=20, font=("bold", 11)).place(x=20, y=90)

entry_1 = Entry(root, textvar=ItemCode, font=("bold", 11)).place(x=180, y=90)

label_2 = Label(root, text="Description", width=20, font=("bold", 11)).place(x=20, y=140)

entry_2 = Entry(root, textvar=Description, font=("bold", 11)).place(x=180, y=140)

label_3 = Label(root, text="Category", width=20, font=("bold", 11)).place(x=20,y=190)

list1 = ['', 'BINDING', 'BOWS', 'CARTONS', 'CHEMICAL']

list = OptionMenu(root, c, *list1)

list.config(width=20)

c.set('CATEGORY')

list.place(x=178, y=190)

label_4 = Label(root, text="Quantity", width=20, font=("bold", 11)).place(x=20, y=290)

entry_4 = Entry(root, textvar=Quantity, font=("bold", 11)).place(x=180, y=290)

label_5 = Label(root, text="Unit", width=20, font=("bold", 11)).place(x=20, y=240)

entry_5 = Entry(root, textvar=Unit, font=("bold", 11)).place(x=180, y=240)

# Style of buttons
style.configure('W.TButton', font=('arial', 10, 'bold'), foreground='red')
style.configure('S.TButton', font=('arial', 10, 'bold'), foreground='blue')

# Inserting buttons
submit1 = Button(root, text='Submit', style='S.TButton', width=11, command=database).place(x=20, y=340)
exit1 = Button(root, text='Exit', style='W.TButton',width=11, command=root.destroy).place(x=260, y=340)


data2()
root.mainloop()

我需要在关闭用户表单脚本后将数量添加到数据库中显示的数量。(显然,根据数学原理,此数量可以增加或减少,如果加上负数,则可以减去。)

理想的情况是,只要树视图脚本正在运行,数据库的连接就应该是连续的。

在这方面的任何帮助将不胜感激。我对特定用语和/或计算以及代码放置位置的了解非常有限。

谢谢您

python python-3.x sqlite tkinter database-connection
1个回答
0
投票

无论记录是否已更新,您都应修改UserForm_Test_Altered_.py以返回值:

from tkinter import *
import sqlite3
from tkinter.ttk import *
import os
import sys

root = Tk()
root.geometry('380x400')
# root.state('zoomed')
root.title("Received")

ItemCode = StringVar()
Description = StringVar()
Quantity = IntVar()
Unit = StringVar()
var = IntVar()
c = StringVar()
var1 = IntVar()
var2 = IntVar()


def database():
    code = ItemCode.get()
    quantity = Quantity.get()

    with sqlite3.connect('MyStock.sql3') as conn:
        cursor = conn.cursor()
        cursor.execute('UPDATE MyStock SET Quantity=? WHERE ItemCode=?', (quantity, code,))
        updated = cursor.rowcount
        cursor.close()
        conn.commit()
        root.destroy()
        sys.exit(updated) # return value whether record has been updated


# Inserting labels and field of input
Label(root, text="Stock Movement", width=20, font=("bold", 20)).place(x=90, y=23)

Label(root, text="Item Code", width=20, font=("bold", 11)).place(x=20, y=90)
Entry(root, textvar=ItemCode, font=("bold", 11)).place(x=180, y=90)

Label(root, text="Description", width=20, font=("bold", 11)).place(x=20, y=140)
Entry(root, textvar=Description, font=("bold", 11)).place(x=180, y=140)

Label(root, text="Category", width=20, font=("bold", 11)).place(x=20,y=190)
list1 = ['', 'BINDING', 'BOWS', 'CARTONS', 'CHEMICAL']
list = OptionMenu(root, c, *list1)
list.config(width=20)
c.set('CATEGORY')
list.place(x=178, y=190)

Label(root, text="Quantity", width=20, font=("bold", 11)).place(x=20, y=290)
Entry(root, textvar=Quantity, font=("bold", 11)).place(x=180, y=290)

Label(root, text="Unit", width=20, font=("bold", 11)).place(x=20, y=240)
Entry(root, textvar=Unit, font=("bold", 11)).place(x=180, y=240)

# Style of buttons
style = Style()
style.configure('W.TButton', font=('arial', 10, 'bold'), foreground='red')
style.configure('S.TButton', font=('arial', 10, 'bold'), foreground='blue')

# Inserting buttons
Button(root, text='Submit', style='S.TButton', width=11, command=database).place(x=20, y=340)
Button(root, text='Exit', style='W.TButton',width=11, command=root.destroy).place(x=260, y=340)

root.mainloop()

然后在main_screen.py中检查os.system(...)的返回值,以确定是否将数据重新加载到树视图中:

from tkinter import *
from tkinter import ttk
import sqlite3
from tkinter.ttk import *
import os

root = Tk()
#root.state('zoomed')
root.title("Inventory Balance")

def load_data():
    tree.delete(*tree.get_children())
    with sqlite3.connect('MyStock.sql3') as conn:
        mycursor = conn.cursor()
        mycursor.execute("SELECT * FROM MyStock")
        for row in mycursor:
            tree.insert('', 'end', values=row[0:6])

frame = Frame(root)
frame.pack()

style = ttk.Style()
style.configure("Treeview.Heading", font=("Arial", 14), foreground="yellow", background="black")

tree = ttk.Treeview(frame, columns=(0, 1, 2, 3, 4), height=30, show="headings")
tree.pack(side='left')

tree.heading(0, text="Item Code")
tree.heading(1, text="Description")
tree.heading(2, text="Category")
tree.heading(3, text="Unit")
tree.heading(4, text="Quantity")

tree.column(0, width=300)
tree.column(1, width=300)
tree.column(2, width=300)
tree.column(3, width=150)
tree.column(4, width=150)

# Inserting Scrollbar
scroll = ttk.Scrollbar(frame, orient="vertical", command=tree.yview)
scroll.pack(side='right', fill='y')

tree.configure(yscrollcommand=scroll.set)

# Configuring style
style = Style()
style.configure('W.TButton', font=('calibri', 10, 'bold', 'underline'), foreground='red')

# Program to open with Button1

def userform1():
    ret = os.system('python UserForm_Test_altered_.py')
    if ret:
        # record updated, reload data
        load_data()

# Inserting Buttons
''' Button 1'''
btn1 = Button(root, text='Exit', style='W.TButton', command=root.destroy)
btn1.pack(side='right')

''' Button 2'''
btn2 = Button(root, text='Issue / Receive', command=userform1)
btn2.pack(side='left')

load_data()

root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.