{"id":3337,"date":"2024-08-29T14:42:09","date_gmt":"2024-08-29T17:42:09","guid":{"rendered":"https:\/\/horatech.shop\/?p=3337"},"modified":"2024-11-29T14:12:06","modified_gmt":"2024-11-29T17:12:06","slug":"como-fazer-uma-interface-grafica-com-python-e-tkinter","status":"publish","type":"post","link":"https:\/\/horatech.shop\/en\/how-to-make-a-graphical-interface-with-python-and-tkinter\/","title":{"rendered":"How to make a graphical interface with Python and Tkinter"},"content":{"rendered":"<p>Creating a python graphical interface with Tkinter is an amazing ability for developers who want to create apps with clear and intuitive visual interaction.<\/p>\n\n\n\n<p>With Tkinter, you can create friendly interfaces efficiently and accessible, allowing programmers at all levels to exploit Python's capabilities. <\/p>\n\n\n\n<p>In this post, we will take a dive into the essential steps to set up a development environment and detail how to implement common interface elements such as buttons, input boxes and menus.<\/p>\n\n\n\n<p>Tkinter is the standard library for python graphic interfaces, being a reliable and widely used choice in the community.<\/p>\n\n\n\n<p>In addition to the basic concepts, the article also brings tips and best practices for a deeper understanding of the creation of effective interfaces, arousing your interest to continue learning and enhancing your software development skills.<\/p>\n\n\n\n<p>To start developing graphic interfaces with Python and Tkinter, the first step is to ensure that your environment is configured correctly.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1024\" height=\"659\" src=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/Como-fazer-uma-interface-grafica-com-Python-e-Tkinter.webp?resize=1024%2C659&#038;ssl=1\" alt=\"\" class=\"wp-image-3338\" srcset=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/Como-fazer-uma-interface-grafica-com-Python-e-Tkinter.webp?resize=1024%2C659&amp;ssl=1 1024w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/Como-fazer-uma-interface-grafica-com-Python-e-Tkinter.webp?resize=300%2C193&amp;ssl=1 300w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/Como-fazer-uma-interface-grafica-com-Python-e-Tkinter.webp?resize=768%2C494&amp;ssl=1 768w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/Como-fazer-uma-interface-grafica-com-Python-e-Tkinter.webp?w=1244&amp;ssl=1 1244w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\"><em>Python and Flask<\/em><\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Python Installation<\/strong><\/h2>\n\n\n\n<p>The first step is to install Python, which can be downloaded from the official Python.org website.<\/p>\n\n\n\n<p>After installation, do a simple test. <code>Python -Version<\/code> or <code>Python3 -Version<\/code>.<\/p>\n\n\n\n<p>Otherwise, check the installation procedure again.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TKINTER LIBRARY INSTALLATION<\/strong><\/h2>\n\n\n\n<p>The Tkinter library is usually installed with Python, but it's always good to check it out. <code>IMPORT TKINTER<\/code>.<\/p>\n\n\n\n<p>If no errors appear, the library is available. <\/p>\n\n\n\n<p>In distributions based in Debian, such as Ubuntu, use the command <code>SUDO APT-GET INSTALL PYTHON3-TK<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TKINTER BASIC CONCEPTS<\/strong><\/h2>\n\n\n\n<p>Tkinter is the default library for creating python graphic interfaces.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1024\" height=\"579\" src=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Biblioteca-Tkinter.webp?resize=1024%2C579&#038;ssl=1\" alt=\"\" class=\"wp-image-3343\" srcset=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Biblioteca-Tkinter.webp?resize=1024%2C579&amp;ssl=1 1024w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Biblioteca-Tkinter.webp?resize=300%2C170&amp;ssl=1 300w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Biblioteca-Tkinter.webp?resize=768%2C434&amp;ssl=1 768w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Biblioteca-Tkinter.webp?w=1472&amp;ssl=1 1472w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\"><em>Tkinter Library<\/em><\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Main window<\/strong><\/h3>\n\n\n\n<p>The main window is the starting point for any Tkinter app. <code>Tk<\/code>, which initializes the application and causes the window to appear automatically.<\/p>\n\n\n\n<p>You can configure various window options such as title and size right after your creation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Import tkinter as tk<\/code><\/pre>\n\n\n\n<p>Here, <code>title<\/code> defines the name of the window and <code>geometry<\/code> Specifies your dimensions. <code>Mainloop ()<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Widgets<\/strong><\/h3>\n\n\n\n<p>Widgets are the components that form the graphic interface.<\/p>\n\n\n\n<p>You add widgets to the main window using the methods <code>Pack ()<\/code>, <code>GRID ()<\/code> or <code>Place ()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>button = tk.button (window, text = \"click here\")<\/code><\/pre>\n\n\n\n<p>Each widget has properties that you can configure, as <code>bg<\/code> for the background color and <code>FG<\/code> for the color of the text.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Events and Bindings<\/strong><\/h3>\n\n\n\n<p>Tkinter events are actions that the user performs, such as mouse clicks or key pressures.<\/p>\n\n\n\n<p>To connect an event to a function, you use the method <code>Bind ()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEF click ():<\/code><\/pre>\n\n\n\n<p>In this example, <code>\"\"<\/code> represents a left click of the mouse. <code>click<\/code> It is called when the event occurs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Drawing the graphic interface<\/strong><\/h2>\n\n\n\n<p>Creating a graphical interface with Python and Tkinter involves planning the disposition of the elements, adding relevant widgets and managing the layout efficiently.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1024\" height=\"579\" src=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Desenhando-a-Interface-Grafica.webp?resize=1024%2C579&#038;ssl=1\" alt=\"\" class=\"wp-image-3342\" srcset=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Desenhando-a-Interface-Grafica.webp?resize=1024%2C579&amp;ssl=1 1024w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Desenhando-a-Interface-Grafica.webp?resize=300%2C170&amp;ssl=1 300w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Desenhando-a-Interface-Grafica.webp?resize=768%2C434&amp;ssl=1 768w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Desenhando-a-Interface-Grafica.webp?w=1472&amp;ssl=1 1472w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\"><em>Drawing the graphic interface<\/em><\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Setting the layout<\/strong><\/h3>\n\n\n\n<p>Start by setting a layout that makes the interface look organized and easy to use.<\/p>\n\n\n\n<p>Widgets can be positioned using percentages or pixels as needed. <code>.Pack ()<\/code>, <code>.Grid ()<\/code> e <code>.place ()<\/code> To organize the components on the screen.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Adding widgets<\/strong><\/h3>\n\n\n\n<p>Widgets are the building blocks of the graphic interface. <code>Label<\/code>, <code>Button<\/code>, <code>Entry<\/code> e <code>Text<\/code>. <code>Label<\/code> Displays text and one <code>Button<\/code> performs actions when clicked.<\/p>\n\n\n\n<p>Add these components to the layout logically, ensuring that each has a clear purpose.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Layout Managers<\/strong><\/h3>\n\n\n\n<p>Layout managers define how widgets behave inside the window. <code>.Pack ()<\/code>, <code>.Grid ()<\/code> e <code>.place ()<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.Pack ()<\/code>: Organizes widgets in the order where they are added, in line or column.<\/li>\n\n\n\n<li><code>.Grid ()<\/code>: Creates a grid where widgets can be positioned on specific lines and columns.<\/li>\n\n\n\n<li><code>.place ()<\/code>: Allows you to put widgets in exact coordinates, giving full control over the position.<\/li>\n<\/ul>\n\n\n\n<p>Choose the layout manager based on the needs of your application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Manipulating input data<\/strong><\/h2>\n\n\n\n<p>Here, we will discuss how to deal with input data on a graphical interface using Python and Tkinter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Text field<\/strong><\/h3>\n\n\n\n<p>Text fields are essential for capturing user data. <code>Entry<\/code> to create a text field.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Import tkinter as tk<\/code><\/pre>\n\n\n\n<p>This creates a field where the user can enter information. <code>GET ()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>value = input.get ()<\/code><\/pre>\n\n\n\n<p>In addition, you can customize the text field with parameters like <code>width<\/code> e <code>bg<\/code> To modify its width and background color, respectively.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Action buttons<\/strong><\/h3>\n\n\n\n<p>Action buttons allow the user to execute commands. <code>Button<\/code> for that.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Botao = tk.button (root, text = \"send\", command = some_function)<\/code><\/pre>\n\n\n\n<p>Here, <code>any_function<\/code> It must be a definite function that will process the data, such as saving or displaying the inputs.<\/p>\n\n\n\n<p>Buttons can also be customized with options like <code>bg<\/code>, <code>FG<\/code> e <code>font<\/code> To change the background color, text color and source style.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Integration with database<\/strong><\/h2>\n\n\n\n<p>Integrating a graphic interface with a database is critical to creating dynamic and functional applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Connecting to the database<\/strong><\/h3>\n\n\n\n<p>To connect to a python database, you can use the library <code>SQLite3<\/code> For SQLite databases.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sqlite3<\/code><\/pre>\n\n\n\n<p>After establishing the connection, create a cursor to interact with the bank and execute SQL commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cursor = connection.cursor ()<\/code><\/pre>\n\n\n\n<p>For other databases, such as MySQL or PostgreSQL, use libraries as <code>MySQL-Connector<\/code> or <code>PSYCOPG2<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>connection.close ()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>CRUD OPERATIONS<\/strong><\/h3>\n\n\n\n<p>CRUD (create, read, update and delete) operations are essential for any app that manipulates data.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>To create<\/strong>: To enter data, use the command <code>Insert<\/code>:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Curso.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>To read<\/strong>: To consult data, use the command <code>Select<\/code>:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Course.Execute (\"Select * From Table\")<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>To update<\/strong>: To modify existing data, use the command <code>Update<\/code>:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>COURSE.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Delete<\/strong>: To remove data, use the command <code>Delete<\/code>:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>COURSE.<\/code><\/pre>\n\n\n\n<p>These operations are the basis for interacting with the database in a TKINTER application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Adding Advanced Resources<\/strong><\/h2>\n\n\n\n<p>Advanced resources can enrich the graphic interface, making it more interactive and efficient.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1024\" height=\"579\" src=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Adicionando-Recursos-Avancados.webp?resize=1024%2C579&#038;ssl=1\" alt=\"\" class=\"wp-image-3341\" srcset=\"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Adicionando-Recursos-Avancados.webp?resize=1024%2C579&amp;ssl=1 1024w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Adicionando-Recursos-Avancados.webp?resize=300%2C170&amp;ssl=1 300w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Adicionando-Recursos-Avancados.webp?resize=768%2C434&amp;ssl=1 768w, https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/08\/Adicionando-Recursos-Avancados.webp?w=1472&amp;ssl=1 1472w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\"><em>Adding Advanced Resources<\/em><\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Charts and data views<\/strong><\/h3>\n\n\n\n<p>Using libraries like Matplotlib and Seabron can transform the interface experience. <code>FigureCanvastkag<\/code>.<\/p>\n\n\n\n<p>Here is a simple example of graph:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Import Matplotlib.pyplot as PLT<\/code><\/pre>\n\n\n\n<p>Thus, dynamic charts can be updated according to the user's inputs, facilitating the interpretation of complex data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>File manipulation<\/strong><\/h3>\n\n\n\n<p>Manipulating files is essential for many applications. <code>filediatialog<\/code>.<\/p>\n\n\n\n<p>Aqui est\u00e1 um c\u00f3digo b\u00e1sico para abrir um arquivo:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>From TKINTER IMPORT FILEDIALOG<\/code><\/pre>\n\n\n\n<p>This allows the user to load and manipulate data directly on the application, making the interface more interactive and functional.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TESTS AND DEGREED<\/strong><\/h2>\n\n\n\n<p>Tests and clearance are fundamental steps in developing a graphic interface with Python and Tkinter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Writing tests for the interface<\/strong><\/h3>\n\n\n\n<p>To test a graphical interface, it is important to create a set of tests that check the application features. <code>Unittest<\/code> e <code>pytest<\/code> They are widely used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import unittest<\/code><\/pre>\n\n\n\n<p>In addition to checking the behavior, tests can also validate if the interface meets usability criteria.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>DEBUGGING OF FUNCTIONS<\/strong><\/h3>\n\n\n\n<p>Debugging is essential to identify problems that are not captured by the tests. <code>PDB<\/code> They facilitate the process, allowing the developer to inspect variables and follow the flow of execution.<\/p>\n\n\n\n<p>A practical method of tkinter clearance is to add <code>print<\/code> statements at strategic points.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEF On_Button_Click (Self):<\/code><\/pre>\n\n\n\n<p>In addition, you can configure a logger to record additional information:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import logging<\/code><\/pre>\n\n\n\n<p>These approaches offer insights on application operation and help in fault correction.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>PACKAGING AND DISTRIBUTION<\/strong><\/h2>\n\n\n\n<p>The packaging and distribution of Python applications created with Tkinter allows developers to easily share their projects.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating Executables<\/strong><\/h3>\n\n\n\n<p>To turn a project into an executable, a popular tool is Pyinstaller.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Install Pyinstaller with <code>Pip Install Pyinstaller<\/code>.<\/li>\n\n\n\n<li>Browse to your script directory.<\/li>\n\n\n\n<li>Run the command <code>Pyinstaller - -INEFILE YOUR_Sript.py<\/code>.<\/li>\n<\/ol>\n\n\n\n<p>This creates a folder <code>distance<\/code>where will be the final executable. <code>cx_freeze<\/code> e <code>py2exe<\/code>but pyinstaller is widely used for its simplicity and efficacy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Applications Distribution<\/strong><\/h3>\n\n\n\n<p>Distribution can occur by various means, such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Personal website<\/strong>: Make the executable available on a website.<\/li>\n\n\n\n<li><strong>Github<\/strong>: Create a page for upload and versions management.<\/li>\n\n\n\n<li><strong>Marketplace<\/strong>: Publish on platforms like Pypi.<\/li>\n<\/ul>\n\n\n\n<p>Also, include a readme file with installation and use instructions.<\/p>\n\n\n\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>Criar uma interface gr\u00e1fica em Python com Tkinter \u00e9 uma habilidade incr\u00edvel para desenvolvedores que querem criar aplicativos com uma intera\u00e7\u00e3o visual clara e intuitiva. Com Tkinter, voc\u00ea consegue criar interfaces amig\u00e1veis de forma eficiente e acess\u00edvel, o que permite que programadores de todos os n\u00edveis explorem as capacidades do Python. Neste post, vamos dar [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":3338,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3337","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programacao"],"blocksy_meta":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/horatech.shop\/wp-content\/uploads\/2024\/09\/Como-fazer-uma-interface-grafica-com-Python-e-Tkinter.webp?fit=1244%2C800&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/posts\/3337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/comments?post=3337"}],"version-history":[{"count":4,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/posts\/3337\/revisions"}],"predecessor-version":[{"id":3781,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/posts\/3337\/revisions\/3781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/media\/3338"}],"wp:attachment":[{"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/media?parent=3337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/categories?post=3337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/horatech.shop\/en\/wp-json\/wp\/v2\/tags?post=3337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}