我只想把产品的产品数量和每单位价格的金额录入为默认值,我该怎么做呢?
程序代码为
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
from tkinter import ttk
import mysql
import mysql.connector
from tkinter import messagebox
import datetime
from datetime import date
from ttkwidgets.autocomplete import AutocompleteCombobox
adde=Tk()
frm3=Frame(adde)
frm3.place(x=191,y=0,relwidth=1,relheight=1)
frm3.configure(bg='#E5E4E2')
# Unit price
price = Label(frm3, text="Unit Price",bg='#E5E4E2', font=("times new roman", 10)).place(x=555,
y=290)
price_01 = Entry(frm3)
price_01.place(x=555, y=310, width=70)
# Quantity
qty = Label(frm3, text="Quantity",bg='#E5E4E2', font=("times new roman", 10)).place(x=635,
y=290)
qty_01 = Entry(frm3)
qty_01.place(x=635, y=310, width=60)
# Amount
amount = Label(frm3, text="Amount",bg='#E5E4E2', font=("times new roman", 10)).place(x=705,
y=290)
amount_01 = Entry(frm3)
amount_01.place(x=705, y=310, width =100)
adde.title(" Voucher Entry ")
adde.geometry("1400x700")
adde.configure(bg='#659EC7')
adde.mainloop()用户输入的价格、数量和金额条目应使用价格和数量的产品自动更新,用户不能编辑该条目...
发布于 2021-12-03 10:46:22
我不完全清楚你需要什么,但如果我理解你,你需要创建function来处理产品的更新。在代码的GUI部分,您需要添加button并将updating funtion传递给它。
button= Button(window, height=1, width=10, text="UPDATE",
command=lambda: update_function(product, amount, price))
button_login.pack()使用.get()从输入字段获取数据的内部函数
示例:
def update_function(product, amount, price):
product_data = product.get()
amount_data = amount.get()
price_data =price.get()
# Rest of your code and updating DB code因此,在上面的示例中,传递给update function的product, amount and price是entry fileds,所以通过使用.get(),您将获得在entry fileds中输入的数据。
发布于 2021-12-03 14:34:25
您可以在price_01和qty_01上绑定<KeyRelease>事件,然后在事件回调中计算金额并更新amount_01。如果你不想让它可编辑,我建议将amount_01从Entry改为Label。
# avoid using wildcard import
import tkinter as tk
def calc_amount(event):
try:
price = float(price_01.get())
qty = int(qty_01.get())
amount_01['text'] = round(price * qty, 2)
except ValueError as ex:
# invalid input in price or qty
# so clear amount_01
amount_01['text'] = ''
adde = tk.Tk()
frm3 = tk.Frame(adde)
frm3.place(x=191, y=0, relwidth=1, relheight=1)
frm3.configure(bg='#E5E4E2')
# Unit price
tk.Label(frm3, text="Unit Price", bg='#E5E4E2', font=("times new roman", 10)).place(x=555, y=290)
price_01 = tk.Entry(frm3)
price_01.place(x=555, y=310, width=70)
price_01.bind('<KeyRelease>', calc_amount) # call calc_amount on key input
# Quantity
tk.Label(frm3, text="Quantity", bg='#E5E4E2', font=("times new roman", 10)).place(x=635, y=290)
qty_01 = tk.Entry(frm3)
qty_01.place(x=635, y=310, width=60)
qty_01.bind('<KeyRelease>', calc_amount) # call calc_amount on key input
# Amount
tk.Label(frm3, text="Amount",bg='#E5E4E2', font=("times new roman", 10)).place(x=705, y=290)
# change amount_01 from Entry to Label, so it is not editable
#amount_01 = Entry(frm3)
amount_01 = tk.Label(frm3, border=1, relief='sunken', bg='white', anchor='w')
amount_01.place(x=705, y=310, width =100)
adde.title(" Voucher Entry ")
adde.geometry("1400x700")
adde.configure(bg='#659EC7')
adde.mainloop()https://stackoverflow.com/questions/70212852
复制相似问题