Curso Python. Interfaces gráficas III. Vídeo 44
Introduction to Python GUI Programming
Overview of Labels in GUI
- The session begins with an introduction to graphical user interfaces (GUIs) in Python, focusing on the 'Label' widget from the Tkinter library.
- Labels are used to display static text or images within a GUI, allowing for text placement without interaction.
- The syntax for creating a label involves specifying a parent container and various optional parameters.
Key Options for Label Configuration
- Common options include:
text: Specifies the displayed text.
image: Allows displaying images instead of text.
bg(background color),fg(foreground/text color), and others control appearance.
- Additional options like
justify,width, andheightcan be set, but width and height are defined in units rather than pixels.
Practical Example: Creating a Label
- A simple example is introduced where the Tkinter library is imported, and a root window is created named "root."
- A frame called "mi_frame" is created within the root window with specified dimensions of 500x400 pixels.
Adding Text to the Frame
- A variable named "mi_label" is created using the Label class, specifying its parent as "mi_frame" and setting its text content.
- An error occurs due to a typo in naming; correcting it allows successful execution, demonstrating how labels adapt their size based on content.
Positioning Labels Correctly
- The initial use of
.pack()causes layout issues; switching to.place()allows precise positioning using x and y coordinates.
Optimizing Code with Ley Bold and Frame Text
Simplifying Code Structure
- The speaker discusses the use of a simplified code structure by eliminating unnecessary variables, specifically using 'ley bold' directly without a separate label variable.
- An example is provided where
x = 100andy = 200are used to demonstrate how to abbreviate code when no label variable is needed.
Customizing Text Properties
- The speaker explains how to change text color from the default black to red using the property
efe, which stands for foreground color, allowing for specific color customization.
- To specify additional text properties like font type and size, the
fontoption is introduced, showing how to set size (e.g., 18) within parentheses.
Advanced Font Customization
- The method for changing both font type and size is elaborated; for instance, using "comic sans" as a font name along with its size in the
fontfunction.
- It’s emphasized that the specified font must be correctly spelled and installed on the user's computer for it to work properly.
Working with Images in Quintero Library
- Transitioning from text to images, it's noted that the Quintero library supports PNG and GIF formats natively; other formats require additional modules.
- Instructions are given on ensuring image files are stored in appropriate directories relative to Python files, highlighting file path considerations.
Implementing Image Display
- A practical example shows creating an image variable using the class
foto, demonstrating how to load an image file by specifying its path correctly.