Got the logic to my question
I'm trying to replace the pack() function with its "equivalent" in grid, and put my learning to the test. For this I have proceeded with the first example provided by @FJSevilla. This is your example:
import tkinter as tk
from tkinter import ttk
class Application(ttk.Frame):
def __init__(self, main_window):
super().__init__(main_window)
main_window.title("Explorador de archivos y carpetas")
s = ttk.Style()
s.configure('My.TFrame', background='red')
self["style"] = 'My.TFrame'
self.btn1 = tk.Button(main_window, text="Botón 1")
self.btn1.pack()
self.btn2 = tk.Button(self, text="Botón 2")
self.btn2.pack()
if __name__ == "__main__":
root = tk.Tk()
root.geometry("600x400")
root.configure(background='green')
Application(root).pack(side="top", fill="both", expand=True)
root.mainloop()
This is my attempt:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# referencias técnicas
# https://es.stackoverflow.com/questions/346000/al-instaniar-widget-pasar-self-o-master-como-padre/346007
# https://recursospython.com/guias-y-manuales/posicionar-elementos-en-tkinter/
import tkinter as tk
from tkinter import ttk
class Application(ttk.Frame):
def __init__(self, main_window):
super().__init__(main_window)
main_window.title("Explorador de archivos y carpetas")
s = ttk.Style()
s.configure('My.TFrame', background='red')
self["style"] = 'My.TFrame'
main_window.columnconfigure(0, weight=1)
main_window.rowconfigure(0, weight=1)
self.btn1 = tk.Button(main_window, text="Boton 1")
# self.btn1.pack()
self.btn1.grid(row=0, column=0, sticky="n")
self.btn2 = tk.Button(self, text="Boton 2")
#self.btn2.pack()
self.btn2.grid(row=0, column=0, sticky="n")
self.grid(sticky="nsew")
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.rowconfigure(1, weight=1) # supongo que en este ejemplo sobra, pero no interfiere
if __name__ == "__main__":
root = tk.Tk()
root.geometry("600x400")
root.configure(background='green')
# Application(root).pack(side="top", fill="both", expand=True)
app = Application(root)
# app.grid(row=0, column=0, sticky="nsew")
# app.pack(side="top", fill="both", expand=True)
root.mainloop()
This is the result I get:
What is it that I don't quite understand and am omitting?
An additional question. :-)
In my code, if in the text of the buttons I use the accented or , or , I get the following error when executing the program:
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xf3 in position 3: invalid continuation byte
I understand that the accented o should not be represented in UTF-8, and I have understood that Python 3 uses, by default, that code in the script.
Again, what do I not quite understand and am I omitting?
You need to combine properly
grid.rowconfigure/grid.columnconfigure
to allow the rows and columns to expand and very importantly, force the frame to expand as much as it can in all four directions withsticky="nsew"
.You do almost everything right, your error is only here:
row 0 is the button row, not the frame row. By giving it more weight, row 0 expands by reducing row 1 (that of the frame) to its minimum possible height. So that it is the one of the frame that expands, you must apply the weight on row 1, not row 0:
The complete code, removing the unnecessary could be:
I have separated the creation of the widget and
Frame
the button of the main window from that of the content of the widget itselfFrame
to encapsulate each widget and make it clearer. I personally prefer to modify the essentials of the parent widget from a child, it makes the class less portable and the code more confusing. However if you want to do everything from the class it is exactly the same:As for the problem with the accent, Python 3 does indeed use UTF-8 both for the source code (default) and for strings (
str
). But remember to save the script with UTF-8 encoding (how it will depend on your IDE/editor), or failing that, declare the appropriate one at the beginning of the script , for example:The byte
0xf3
is not a valid byte for UTF-8, but it is the or in Latin1/ISO 8859-1: