E
You can effectively use a scroll bar, however it is not as simple as it may seem because not all widgets support them. Among the widgets that if they do are ListBox, Text, Entry and Canvas. What is usually done in these cases is to use a Canvas as a container and associate the scroll bars to the same. A mediumly simple way in your case is to use a Frame that contains all the widgets of your interface and this frame assign it to the canvas.import tkinter as tk
def mostrar_entradas_compra():
categoria = tk.Tk()
categoria.geometry("1366x768")
categoria.title("Categoría")
canvas = tk.Canvas(categoria)
frame = tk.Frame(canvas)
vertscroll = tk.Scrollbar(canvas, orient='vertical', command=canvas.yview)
canvas.configure(yscrollcommand=vertscroll.set)
def on_mouse_scroll(event):
if event.delta:
canvas.yview_scroll(-1 * (event.delta / 120), 'units')
else:
canvas.yview_scroll(1 if event.num == 5 else -1, 'units')
categoria.bind('<Configure>', lambda _: canvas.configure(scrollregion=canvas.bbox("all")))
categoria.bind('<MouseWheel>', lambda event: on_mouse_scroll(event))
categoria.bind('<Button-4>', lambda event: on_mouse_scroll(event))
categoria.bind('<Button-5>', lambda event: on_mouse_scroll(event))
canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
canvas.create_window((0, 0), window=frame, anchor="nw")
vertscroll.pack(side=tk.RIGHT, fill=tk.Y)
tk.Label(frame, text="Flautas de papa con con chorizo",
font=10).grid(row=0, column=0, columnspan=2, sticky="W")
entrada_photo_1 = tk.PhotoImage(file="flautasdechorizo.png")
tk.Label(frame, image=entrada_photo_1).grid(row=1, column=0)
tk.Label(frame, font=9,
text=("Descripción: Se ponen a cocer las papas en agua y sal."
" Cuando están listas se hacen puré.\nSe sofríe cebolla,"
" posteriormente se agrega chorizo.\nSe forman flautas con"
" puré y se rellenan de carne y cebolla.\nLuego se fríen,"
" se sirven y se decora con lechuga y cebolla.\n"
"Precio unitario: 1100 colones")).grid(row=1, column=1)
tk.Label(frame, text="Pico de gallo con nachos",
font=10).grid(row=3, column=0, columnspan=2, sticky="W")
entrada_photo_2 = tk.PhotoImage(file="piconachos.png")
tk.Label(frame, image=entrada_photo_2).grid(row=4, column=0)
tk.Label(frame, font=9,
text=("Descripción: Elaborado al picar tomate sin semillas,"
" cebolla morada, culantro, chile jalapeño,\ndiente ajo"
" picado finamente, limón. Se sirve acompañado de nachos.\n"
"Precio unitario: 1500 colones")).grid(row=4, column=1)
tk.Label(frame, text="Sopa de elote",
font=10).grid(row=5, column=0, columnspan=2, sticky="W")
entrada_photo_3 = tk.PhotoImage(file="SopadeElote.png")
tk.Label(frame, image=entrada_photo_3).grid(row=6, column=0)
tk.Label(frame, font=9,
text=("Descripción: Se elabora a partir de sofreír cebolla,"
" hervir leche. A esta leche se le agrega la\n cebolla,"
" consomé de pollo, y el elote luego se cocina hasta hervir\n"
"y se sazona con sal y pimienta. Se utiliza queso como"
" guarnición.Precio unitario: 1450 colones.")).grid(row=6, column=1)
tk.Label(frame, text="Crema de almejas",
font=10).grid(row=7, column=0, columnspan=2, sticky="W")
entrada_photo_4 = tk.PhotoImage(file="CremaAlmeja.png")
tk.Label(frame, image=entrada_photo_4).grid(row=8, column=0)
tk.Label(frame, font=9,
text=("Descripción: Elaborado a partir de derretir mantequilla, "
"agregar cebolla y apio, se sofríe. \n Se agrega harina,"
" y se mezcla, se vierte caldo de pescado, jugo de \n"
"almejas, y se sazona al gusto. Luego se agrega la leche,"
" la crema y las\n almejas, se cocina sin dejar que hierva.\n"
"Precio unitario: 1800 colones")).grid(row=8, column=1)
tk.Label(frame, text="Sopa de lentejas con chorizo",
font=11).grid(row=9, column=0, columnspan=2, sticky="W")
entrada_photo_5 = tk.PhotoImage(file="SopaLenteja.png")
tk.Label(frame, image=entrada_photo_5).grid(row=10, column=0)
tk.Label(frame, font=11,
text=("Descripción: Deliciosa sopa de lentejas calientita,"
" con chorizo y tocino acompañado cilantro en\n caldillo de"
" jitomate con un toque picosito de chile ancho. Servida de\n"
" plátanos machos deshidratados y queso panela."
"Precio unitario: 1600 colones.")).grid(row=10, column=1)
categoria.mainloop()
if name == "main":
mostrar_entradas_compra()
It has also been added to the support bar to scroll with the mouse wheel.A few observations:I don't know how to import tkinter, but by your code I deduce you to import it in several ways. You should import it once and not use it. from tkinter import *It's a bad practice.Positioning by place is useful in some cases, especially when creating custom widgets. However, in this case I think grid It's a lot better choice, to start your code is broken down as soon as the source changes (which can happen when you run it in another system simply) or as soon as an image occupies more account space. On the other hand, it is very cumbersome to be measuring pixels to place everything in place. If there's nothing to stop you, I advise you to use grid as I do in the previous code.You shouldn't. var = tk.Label(...).place(...). If you do this, var will refer to the method output placewho returns None. This makes the variable completely useless, if you do not need to make future reference to the instance simply do:tk.Label(...).place(...)
If you need the reference for the future, separate the call place/grid/pack of the instance:label = tk.Label(...)
label.place(...)
I've shortened the lines so the code is more readable. There are more ways to work with long string verbatims, including using multiline strings with triple quotes. Apart from this there are other modifications for the code to follow as far as possible the style conventions ( https://www.python.org/dev/peps/pep-0008/ ), for example, the names in uppercase and CamelCase should be reserved to name classes, for variables and functions it is used lowercase and _.