I’m trying to put two Labels next to each other but I want one to cover half the window and the other the other half but I’ve tried everything but nothing happens they just stay in that corner
I wanted them to look the way I draw them in the image but they just stay there in the corner
from textwrap import fill
import tkinter as tk
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from PIL import Image, ImageTk
class APP:
def __init__(self):
# Criando a janela principal
self.root = tk.Tk()
self.root.title("FMD")
self.root.geometry("1080x600") # Define o tamanho da janela
# Impede o redimensionamento da janela
self.root.resizable(False, False)
# Carregando a imagem existente
background_image = Image.open("background.png")
# Convertendo a imagem para o formato Tkinter PhotoImage
self.background_photo = ImageTk.PhotoImage(background_image)
# Criando um rótulo com a imagem como plano de fundo
self.background_label = tk.Label(
self.root, image=self.background_photo)
self.background_label.place(x=0, y=0, relwidth=1, relheight=1)
# Criando o frame do Dataset
self.dataset_frame = ttk.Frame(self.root, bootstyle="dark")
self.dataset_frame.grid(row=0, column=0)
self.dataset_label = ttk.Label(
self.dataset_frame, text="Dataset:", bootstyle="inverse-dark")
self.dataset_label.configure(font=("Arial", 16, "bold"))
self.dataset_label.pack(side=LEFT)
# Criando o frame do preview
self.preview_frame = ttk.Frame(self.root, bootstyle="dark")
self.preview_frame.grid(row=0, column=1)
self.preview_label = ttk.Label(
self.preview_frame, text="Preview:", bootstyle="sucess")
self.preview_label.configure(font=("Arial", 16, "bold"))
self.preview_label.pack(side=LEFT)
# Inicia o loop principal da aplicação
self.root.mainloop()
# Inicializa a aplicação
app = APP()
I want one to cover half the window and the other the other half but
I’ve tried everything but nothing happens they just stay in that
corner
The problem can be fixed.
- Add
width
andanchor
in Label widgets.
Snippet:
self.dataset_label = ttk.Label(self.dataset_frame,
width=50, anchor="n", text="Dataset:",
bootstyle="inverse-dark")
and:
self.preview_label = ttk.Label(self.preview_frame,
width=50, anchor="n",
text="Preview:",
bootstyle="sucess")
Screenshot:
I switched Grid to pack ( please anyone correct me if I am wrong but I could not find a way to ‘easily’ configure the grid column ).
By using winfo_width()
you can get the width of the window/object.
from tkinter import *
#Tendrás que volver a agregar todas las imágenes e
# importaciones, las eliminé para poder probarlas.
class APP:
def __init__(self):
# Criando a janela principal
self.root = Tk()
self.root.title("FMD")
self.root.geometry("1080x600") # Define o tamanho da janela
# Impede o redimensionamento da janela
self.root.resizable(False, False)
# Criando o frame do Dataset
# Esto actualiza los datos 'raíz' utilizados para obtener el ancho.
self.root.update()
# A continuación, obtenga el ancho de la aplicación y divídalo por dos.
self.dataset_frame = Frame(self.root, width=round(self.root.winfo_width()/2), height=30)
self.dataset_frame.pack(side=LEFT, anchor=NW)
# Esto está aquí para que el marco no colapse (sin esta línea el marco tendrá un tamaño: 0x0)
self.dataset_frame.pack_propagate(False)
self.dataset_label = Label(self.dataset_frame, text="Dataset:", font=("Arial", 16, "bold"))
self.dataset_label.pack()
# Criando o frame do preview
self.preview_frame = Frame(self.root, width=round(self.root.winfo_width()/2), height=30)
self.preview_frame.pack(side=LEFT, anchor=NW)
self.preview_frame.pack_propagate(False)
self.preview_label = Label(self.preview_frame, text="Preview:", font=("Arial", 16, "bold"))
self.preview_label.pack()
# Inicia o loop principal da aplicação
self.root.mainloop()
# Inicializa a aplicação
app = APP()