Tkinter and SQLite database with Python

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the primary purpose of using Tkinter in conjunction with SQLite, as described in the document?

  • To bypass the need for SQL queries in Python.
  • To enhance the security of SQLite databases.
  • To develop a visual application for interacting with an SQLite database. (correct)
  • To create complex data models within SQLite.

According to the document, how is the project structured to manage the application's complexity?

  • By using external libraries to handle both the GUI and database interactions within the same file.
  • By using a single module for all functionalities.
  • By integrating SQLite database operations directly into the Tkinter interface code.
  • By dividing the application into 'formularioarticulos.py' for presentation logic and 'articulos.py' for data access. (correct)

What is the main role of the formularioarticulos.py module in the described application?

  • To handle the presentation logic and user interface elements. (correct)
  • To define SQL queries for data manipulation.
  • To manage SQLite database connections.
  • To directly store and retrieve data from the database.

What methods should be invoked to correctly display data in the scrolledtext widget?

<p><code>insert()</code> (A)</p> Signup and view all the answers

What is the primary role of the articulos.py module?

<p>Providing data access and manipulation methods for the SQLite database. (C)</p> Signup and view all the answers

Which GUI component from the tkinter library is used to organize different sections/pages in the application's interface?

<p><code>Notebook</code> (B)</p> Signup and view all the answers

What is the correct order of operations when adding data to the database?

<p>open connection -&gt; create cursor -&gt; execute query -&gt; commit changes -&gt; close connection (D)</p> Signup and view all the answers

When defining the SQL query for inserting a new article, what is the '?' symbol used for?

<p>To represent a placeholder for values that will be passed as parameters. (A)</p> Signup and view all the answers

What method is called to ensure data is written to the database?

<p><code>commit()</code> (A)</p> Signup and view all the answers

What is the purpose of the StringVar() class from tkinter?

<p>To manage and track changes to text in <code>Entry</code> widgets. (C)</p> Signup and view all the answers

What should you do after calling the method consulta in the articulos.py module?

<p>Set each record to be displayed. (C)</p> Signup and view all the answers

According to the document, what argument is necesary when creating only one datum?

<p>A tuple (D)</p> Signup and view all the answers

What is the function of the method recuperar_todos?

<p>To obtain the data on the registers. (B)</p> Signup and view all the answers

What method is used to remove the values scrolledtext1 widget?

<p><code>delete()</code> (C)</p> Signup and view all the answers

What code is missing in the alta method?

def alta(self, datos):
 cone=self.abrir()
 cursor=cone.cursor()
 sql="insert into articulos(descripcion, precio) values (?,?)"
 cursor.execute(sql, datos)
 #MISSING CODE
 cone.close()

<p>cone.commit() (D)</p> Signup and view all the answers

In the context of the Tkinter application described, what is the significance of ensuring that the main module formularioarticulos.py is executed?

<p>It sets up the user interface and starts the application's event loop, making the GUI accessible. (C)</p> Signup and view all the answers

How are database connection strings handled to ensure the application can locate the SQLite database file?

<p>The path to the database file is explicitly specified in the <code>sqlite3.connect()</code> function. (C)</p> Signup and view all the answers

Consider a scenario where you want to add functionality to delete a particular article from the 'articulos' table based on its code. What additional steps would be necessary beyond modifying the GUI in formularioarticulos.py?

<p>You would need to implement a deletion method in the <code>articulos.py</code> module and call it from <code>formularioarticulos.py</code>. (D)</p> Signup and view all the answers

If the application needs to be extended to allow users to modify the 'descripcion' and 'precio' of an existing article, what is the most efficient approach to implement this functionality?

<p>Implement a new GUI tab with entry fields for the article's code, a 'Consultar' button to load existing data, and entry fields for the new 'descripcion' and 'precio', along with an 'Update' button. (B)</p> Signup and view all the answers

What additional GUI component and method would be necessary to see each record's name, description, and price?

<p>Using a <code>ScrolledText</code> Widget and the <code>insert</code> method. (C)</p> Signup and view all the answers

Flashcards

What is a DBMS?

A system for managing databases, allowing users to interact with data.

What is SQLite?

A free and open source database management system contained in a C programming library.

What is Tkinter?

A Python library used for creating graphical user interfaces.

What does 'alta' method do?

To add new data to a database, often through SQL INSERT statements.

Signup and view all the flashcards

What does 'recuperar_todos' do?

To get a list of data records from a database table.

Signup and view all the flashcards

What is 'formularioarticulos.py'?

The primary module used to create the GUI and interact with the database.

Signup and view all the flashcards

What is 'articulos.py'?

A module containing functions for direct database access and SQL interactions.

Signup and view all the flashcards

What is 'consulta' for?

This method is used to retrieve specific data from the database based on a given condition.

Signup and view all the flashcards

Study Notes

  • The document is about creating a visual interface with Tkinter and accessing an SQLITE database with Python

Introduction

  • It is often necessary to access an SQLite database from an application with a visual interface

Problem

  • To develop a visual application with the Tkinter library that can implement algorithms for loading articles, consulting by code, and complete listing

Solution

  • Work with the Data Base Management System (DBMS) using BD Browser from SQLite
  • The creation of the database named “db_bodega.db” will take place

Table Creation

  • Creating a table called ‘artículos’
  • Table fields include codigo, descripcion, and precio

Visual Interface

  • Visual interface includes loading items, a search function and finally a complete list

Project Organization

  • To organize create two modules named ‘formularioarticulos.py’ and ‘articulos.py’
  • The primary step is to create the directory “proyecto4” where the two modules reside

Module formularioarticulos.py

  • The module ‘formularioarticulos.py’ handles the data presentation and interacts with the second module to access the SQLite database
  • The main steps of the module are: importing the correct tkinter components, creation of the “Articulos” class and the presentation of the loading screen, search and lists

Module articulos.py

  • The module ‘articulos.py’ handles the logic to access the SQLite database
  • The main steps of the module are: importing the correct sqlite components, creation of the “Articulos” class and defining the methods to open, search, add and list the information held in the SQLITE database.

Main

  • Ensure the main module located in the file ‘formularioarticulos.py’ is executed

Code Analysis

  • import tkinter as tk, from tkinter import ttk, from tkinter import messagebox as mb, from tkinter import scrolledtext as st are the modules needed to implement the visual interface.
  • import articulos; the fundamental import is the ‘articulos.py’ module, where the ‘Articulos’ class, the database connection, is implemented.
  • The visual class is called 'FormularioArticulos'. Within the init constructor, an object of the 'Articulos' class is instantiated

Class Notebook

  • The calls in init create each of the pages of the 'Notebook' class object.
  • Create each notebook option for the GUI with the Tkinter framework
  • self.ventana1=tk.Tk()
  • self.ventana1.title("Mantenimiento de artículos")
  • self.cuaderno1 = ttk.Notebook(self.ventana1)
  • self.carga_articulos()
  • self.consulta_por_codigo()
  • self.listado_completo()
  • self.cuaderno1.grid(column=0, row=0, padx=10, pady=10)
  • self.ventana1.mainloop()
  • When the "Carga de artículos"(add articles) tab is selected followed by the "Confirmar" button, the first action is the creation of a tuple, composed of the two data entered in the "Entry" control:
  • def agregar(self):
  • datos=(self.descripcioncarga.get(), self.preciocarga.get())
  • After the collection of data, the method alta of the "articulo1" object is called, passing a tuple with the information to be added.
  • self.articulo1.alta(datos)
  • mb.showinfo("Información", "Los datos fueron cargados")
  • self.descripcioncarga.set("")
  • self.preciocarga.set('')
  • It is appropriate at this moment to analyze the "alta" method of the 'Articulos' class, located in a different module.
  • def alta(self, datos):
  • cone=self.abrir()
  • cursor=cone.cursor()
  • sql="insert into articulos(descripcion, precio) values (?,?)"
  • cursor.execute(sql, datos)
  • cone.commit()
  • cone.close()
  • In the method 'alta', the SQLite connection is opened, a cursor is created and the execute method called to execute the SQL 'insert' command and the information to be added. The connection needs to commit for the data to be added.
  • The connection is openned with: -def abrir(self): -conexion=sqlite3.connect(“bd_bodega.db") -return conexión
  • Specifying the entire address where the "bd_bodega.db" is located is of utmost importance in any Python program where the other folder is located.

Consulting the code

  • When the "Consultar" button is pressed, the following method is executed:
  • def consultar(self):
  • datos=(self.codigo.get(), )
  • respuesta=self.articulo1.consulta(datos)
  • if len(respuesta)>0:
  • self.descripcion.set(respuesta[0][0])
  • self.precio.set(respuesta[0][1])
  • else:
  • self.descripcion.set('')
  • self.precio.set('')
  • mb.showinfo("Información", "No existe un artículo con dicho código")
  • A tuple with only one element consisting of the value is created; it is mandatory that a comma (,) is employed so Python can interpret the information as a tuple.
  • datos=(self.codigo.get(), )
  • The method ‘consulta’ that is related to the ‘Articulos’ class that can be found in another module is executed. The method is expected to list any code that is either empty or with the tuple inside.
  • The method ‘consulta’ that is related to the class ‘Articulos’ calls ‘fetchall’ from the respective cursor
  • def consulta(self, datos):
  • try:
  • cone=self.abrir()
  • cursor=cone.cursor()
  • sql="select descripcion, precio from articulos where codigo=?"
  • cursor.execute(sql, datos)
  • return cursor.fetchall()
  • finally:
  • cone.close()

Complete listing

  • To show all the rows in the table "articulos", an object of the class "scrolledtext" needs to be in place.
  • def listar(self):
  • respuesta=self.articulo1.recuperar_todos()
  • self.scrolledtext1.delete("1.0", tk.END)
  • for fila in respuesta:
  • self.scrolledtext1.insert(tk.END, "código:"+str(fila[0])+
  • "\ndescripción:"+fila[1]+
  • "\nprecio:"+str(fila[2])+"\n\n")
  • The method "recuperar_todos" that is related to the "Articulos" class is called with the information retrieved from the data table.
  • def recuperar_todos(self):
  • try:
  • cone=self.abrir()
  • cursor=cone.cursor()
  • sql="select codigo, descripcion, precio from articulos"
  • cursor.execute(sql)
  • return cursor.fetchall()
  • finally:
  • cone.close()

Proposed Problem

  • Add two tabs to the article management program to allow deleting an article by entering its code and another option to query and modify the description and price of an article.
  • The visual interfaces to implement are:
    • A maintenance view that allows for deleting articles. Requires an input for article code and a "Borrar" button.
  • A maintenance section for modification to the existing articles where the code, description and price can be altered. Requires a “consultar” and “modificar” button

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Tkinter Basics Quiz
11 questions

Tkinter Basics Quiz

AbundantGoblin avatar
AbundantGoblin
Tkinter Widgets Overview
5 questions

Tkinter Widgets Overview

WellPositionedSard9335 avatar
WellPositionedSard9335
Introduction to Tkinter
4 questions

Introduction to Tkinter

CompliantSunflower avatar
CompliantSunflower
Use Quizgecko on...
Browser
Browser