Home > Mobile >  Product of two input in another entry field as default value
Product of two input in another entry field as default value

Time:12-04

I just want to product of item quantity and price per unit into amount entry as default value how can i do that...

program codes are

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()

The user input Price and Quantity and Amount entry should be automatically update with product of price and quantity and that entry can't be editable by user....

CodePudding user response:

It is not completly clear to me what do you need, but if I understood you, you need to create function that will handle updating of the product. In the GUI part of the code you need to add button and pass updating funtion to it.

button= Button(window, height=1, width=10, text="UPDATE",
                          command=lambda: update_function(product, amount, price))
button_login.pack()

Inside function to get data from imput field use .get()

example:

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

So in example above, product, amount and price passed to the update function are entry fileds, so by using .get() you are geting data that was inputed inside entry fileds.

CodePudding user response:

You can bind <KeyRelease> event on price_01 and qty_01, then calculate the amount in the event callback and update amount_01. I would suggest to change amount_01 from Entry to Label if you don't want it editable.

# 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()
  • Related