Python Forum
Problem using a button with tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem using a button with tkinter
#1
I'm new on Python and now I'm trying to do a simple product registration system with tkinter and sqlite3, following a tutorial on the internet. But I'm having some problems and I don't know what to do.

My code:

from tkinter import *
import sqlite3
import tkinter.messagebox


conn = sqlite3.connect("C:\TkinterVenda\Database\store.db")
c = conn.cursor()

result = c.execute("SELECT MAX(id) from inventory")
for r in result:
    ID = r[0]

class Database:

    def __init__(self, master, *args, **kwargs):

        self.master = master
        self.heading = Label(master, text="Cadastro De Produtos", font = ('arial 40 bold'), fg = 'steelblue')
        self.heading.place(x=400,y=0)

        self.Name_1 = Label(master, text = "Nome do produto:", font= ('arial 18 bold'))
        self.Name_1.place(x=0, y=70)

        self.Stock_1 = Label (master, text="Estoque:", font=('arial 18 bold'))
        self.Stock_1.place (x=0, y=120)

        self.CP_1 = Label (master, text="Preço Custo:", font=('arial 18 bold'))
        self.CP_1.place (x=0, y=170)

        self.SP_1 = Label (master, text="Preço Venda:", font=('arial 18 bold'))
        self.SP_1.place (x=0, y=220)

        self.Vendor_1 = Label (master, text="Nome do Fornecedor:", font=('arial 18 bold'))
        self.Vendor_1.place (x=0, y=270)

        self.Vendor_PhoneNumber_1 = Label (master, text="Telefone do Fornecedor", font=('arial 18 bold'))
        self.Vendor_PhoneNumber_1.place (x=0, y=320)

        self.ID_1 = Label (master, text="ID:", font=('arial 18 bold'))
        self.ID_1.place (x=0, y=370)

        self.Name_e = Entry(master, width = 25, font=('arial 18 bold'))
        self.Name_e.place (x=300, y=70)

        self.Stock_e = Entry (master, width=25, font=('arial 18 bold'))
        self.Stock_e.place (x=300, y=120)

        self.CP_e = Entry (master, width=25, font=('arial 18 bold'))
        self.CP_e.place (x=300, y=170)

        self.SP_e = Entry (master, width=25, font=('arial 18 bold'))
        self.SP_e.place (x=300, y=220)

        self.Vendor_e = Entry (master, width=25, font=('arial 18 bold'))
        self.Vendor_e.place (x=300, y=270)

        self.Vendor_PhoneNumber_e = Entry (master, width=25, font=('arial 18 bold'))
        self.Vendor_PhoneNumber_e.place (x=300, y=320)

        self.ID_e = Entry (master, width=25, font=('arial 18 bold'))
        self.ID_e.place (x=300, y=370)

        self.btn_add =  Button(master, text = "Cadastrar", width=25, height = 2, bg='steelblue',fg = 'black', command = self.get_items)
        self.btn_add.place (x=520, y=420)

        self.btn_clear = Button (master, text="Limpar", width=18, height=2, bg='red', fg='black', command = self.clear_all)
        self.btn_clear.place (x=350, y=420)

        self.textBox = Text (master, width=60, height = 18)
        self.textBox.place (x=750, y=70)
        self.textBox.insert(END, "Ultimo ID Cadastrado: " + str(ID))

        self.master.bind('<Return>',self.get_items)
        self.master.bind('<Up>', self.clear_all)

    def get_items(self, *args, **kwargs):

        self.Name = self.Name_e.get()
        self.Stock = self.Stock_e.get()
        self.CP = self.CP_e.get()
        self.SP = self.SP_e.get()
        self.Vendor = self.Vendor_e.get()
        self.Vendor_PhoneNumber = self.Vendor_PhoneNumber_e.get()

        self.TotalCP = float(self.CP) * float(self.Stock)
        self.TotalSP = float(self.SP) * float(self.Stock)
        self.Assumed_profit = float(self.TotalSP - self.TotalCP)

        if self.Name =='' or self.Stock == '' or self.CP == '' or self.SP =='':
            tkinter.messagebox.showinfo("Provedor", "FAVOR PREENCHER TODOS OS CAMPOS!")
        else:
            sql = "INSERT INTO inventory (Name, Stock, CP, SP, Vendor, Vendor_PhoneNumber, TotalCP, TotalSP, Assumed_profit) VALUES(?,?,?,?,?,?,?,?,?)"
            c.execute(sql,(self.Name, self.Stock, self.CP, self.SP, self.Vendor, self.Vendor_PhoneNumber, self.TotalCP, self.TotalSP, self.Assumed_profit))
            conn.commit()

            self.textBox.insert(END, "\n\nCadastro" + str(self.Name) + "no banco de dados com o ID:" + str(ID))
            tkinter.messagebox.showinfo ("Provedor", "CADASTRO FEITO COM SUCESSO!")

    def clear_all(self, *args, **kwargs):
        num = ID + 1
        self.Name_e.delete(0,END)
        self.Stock_e.delete(0,END)
        self.CP_e.delete(0,END)
        self.SP_e.delete(0,END)
        self.Vendor_e.delete(0,END)
        self.Vendor_PhoneNumber_e.delete(0,END)


root = Tk()
b = Database(root)

root.geometry("1366x768+0+0")
root.title("FORMULARIO DE CADASTRO")
root.mainloop()
Error:
Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Cliente\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "C:/TkinterVenda/Database/add_to_db.py", line 85, in get_items self.TotalCP = float(self.CP) * float(self.Stock) ValueError: could not convert string to float: ''
Reply
#2
What is it that you are entering into the entry that you are trying to convert to a float?
Reply
#3
(Jul-02-2020, 07:40 PM)Yoriz Wrote: What is it that you are entering into the entry that you are trying to convert to a float?

CP and SP are the Cost Price and Sell Price
Reply
#4
But whatever you are entering cannot be converted into a float
Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> float('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float:
>>>  
what values have you typed into your entrys
print self.CP & self.Stock before attempting to convert them to a float, what values do they have?
Reply
#5
(Jul-02-2020, 07:47 PM)Yoriz Wrote: But whatever you are entering cannot be converted into a float
Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> float('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float:
>>>  
what values have you typed into your entrys
print self.CP & self.Stock before attempting to convert them to a float, what values do they have?

Sorry, I have expressed in a bad way these errors. It happens when I doesn't enter with one of these fields (CP or SP). When I don't fill them, it's expected to show a message box saying "PLEASE FILL ALL FIELDS" (POR FAVOR PREENCHA TODOS OS CAMPOS). When i fill everything, it almost works. Just the ID number doesn't change.
Reply
#6
Move your conversions to the start of the else statement, so you are checking for valid input before trying to convert.
Reply
#7
(Jul-02-2020, 07:58 PM)Yoriz Wrote: Move your conversions to the start of the else statement, so you are checking for valid input before trying to convert.

Oh, it was just it. Thank you very much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to open a popup window in tkinter with entry,label and button lunacy90 1 872 Sep-01-2023, 12:07 AM
Last Post: lunacy90
  Tkinter button images not showing up lunacy90 7 1,569 Aug-31-2023, 06:39 PM
Last Post: deanhystad
Bug tkinter.TclError: bad window path name "!button" V1ber 2 777 Aug-14-2023, 02:46 PM
Last Post: V1ber
  tkinter layout problem agentperry 4 1,662 Jan-26-2022, 08:18 PM
Last Post: BashBedlam
  Closing Threads and the chrome window it spawned from Tkinter close button law 0 1,700 Jan-08-2022, 12:13 PM
Last Post: law
  tkinter auto press button kucingkembar 2 3,166 Dec-24-2021, 01:23 PM
Last Post: kucingkembar
  problem with libspatialindex using tkinter Timych 1 1,753 Aug-05-2020, 11:39 AM
Last Post: buran
  Use a button in Tkinter to run a Python function Pedroski55 4 3,254 Jun-28-2020, 05:02 AM
Last Post: ndc85430
  Function assigned at a button in tkinter riccardoob 9 4,163 Oct-06-2019, 11:14 AM
Last Post: riccardoob
  tkinter button executes button before being clicked SheeppOSU 1 3,777 Apr-01-2019, 10:51 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020