Jul-02-2020, 07:35 PM
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:
Error:
My code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
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: 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: ''