我只想將專案數量和單位價格的乘積作為默認值輸入金額條目,我該怎么做...
程式代碼是
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()
用戶輸入的價格和數量以及數量條目應自動更新價格和數量的產品,并且該條目不能由用戶編輯....
uj5u.com熱心網友回復:
我不完全清楚您需要什么,但如果我理解您,您需要創建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
因此,在上面的示例中,product, amount and price傳遞給 u pdate functionare entry fileds,因此通過使用.get()您正在獲取輸入到entry fileds.
uj5u.com熱心網友回復:
您可以<KeyRelease>在price_01and上系結事件qty_01,然后在事件回呼和 update 中計算數量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://www.uj5u.com/shujuku/375696.html
