Home > Back-end >  two labels side by side in loop using tkinter in pack function
two labels side by side in loop using tkinter in pack function

Time:09-10

i am new in programming and in tkinter , i made a two windows with grid function and in a loop a label and entry i want both to be side by side using pack function , tried every option , the only thing works is the normal pack but it locate the input after the label not at its side how to fix it

`

from csv import Dialect
import tkinter as tk
from PIL import ImageTk
from tkmacosx import Button
import sqlite3
import csv 


# define variables
# bg = "#03dffc"
# bg = "#022226"
bg = "#5bc6de"

#define function that write data to csv file or excel 
def write_to_csv(all_names):
    with open("inventory.csv" , "w") as f :
        w = csv.writer(f, dialect="excel")
        for name in all_names:
            w.writerow(name)


#define function to clear the previous widget when load the new one 
def clear_widget(frame):
    for widget in frame.winfo_children():
        widget.destroy()

#function to fetch data from database 
def fetch_db():
    connection = sqlite3.connect("data/inventory.db")
    cursor = connection.cursor()
    cursor.execute("SELECT NAME FROM inventory1 ORDER BY RANDOM() LIMIT 10 ;")
    all_names = cursor.fetchall()
    connection.close()
    return all_names

# def pre_process(all_names):
#     names = all_names

def load_frame1():
    #destroy other fram
    clear_widget(frame2)
    #adjust frame order
    frame1.tkraise()
    #to avoid any widget corraption 
    frame1.pack_propagate(False)
        # add logo image as a widget to frame1
    logo_img = ImageTk.PhotoImage(file="assets/pharmacy3.png")
    logo_widget = tk.Label(frame1,image=logo_img, bg=bg)
    logo_widget.image = logo_img
    logo_widget.pack(pady=20)

    #add text as instraction so text as a widget 
    tk.Label(
        frame1,
        text="Press Generate for random product list to review and print",
        bg = bg ,
        fg = "white",
        font=("TKMenuFont", 14)

    ).pack(pady=20)

    #create a button widget 
    Button(
        frame1,
        text="Generate",
        bg = "#155361",
        fg = "white" ,
        font=("TKMenuFont" , 22 ),
        activebackground="#1898b5",
        activeforeground="black",
        cursor="hand",
        borderless = 1 ,
        command=lambda:load_frame2()
    ).pack(pady=20)




def load_frame2():
    #destroy other fram
    clear_widget(frame1)
    #adjust frame order
    frame2.tkraise()
    #retuen fetched data 
    all_names = fetch_db()
    # names = pre_process(all_names)
    logo_img = ImageTk.PhotoImage(file="assets/pharmacy2.png")
    logo_widget = tk.Label(frame2,image=logo_img, bg=bg)
    logo_widget.image = logo_img
    logo_widget.pack(pady=20)

    for i in all_names :
        tk.Label(
            frame2,
            text=i,
            bg = "#155361" ,
            fg = "white",
            font=("TKMenuFont", 10),
       ).pack()
        tk.Entry(
            frame2 ,
            width = 10 ,
        ).pack()

    Button(
        frame2,
        text="Back",
        bg = "#155361",
        fg = "white" ,
        font=("TKMenuFont" , 22 ),
        activebackground="#1898b5",
        activeforeground="black",
        cursor="hand",
        borderless = 1 ,
        command=lambda:load_frame1()
    ).pack(pady=20)
    Button(
        frame2,
        text="Save file",
        bg = "#155361",
        fg = "white" ,
        font=("TKMenuFont" , 22 ),
        activebackground="#1898b5",
        activeforeground="black",
        cursor="hand",
        borderless = 1 ,
        command=lambda:write_to_csv(all_names)
    ).pack(pady=20)


# initiallize app
root = tk.Tk()

# add a title to the screen
root.title("Hafez Pharmacy Inventory Check")


root.eval("tk::PlaceWindow . upper")
# another approch to center the screen
# x = root.winfo_screenwidth() // 2
# y = int(root.winfo_screenheight() * 0.2)
# root.geometry("500x500 "   str(x)   " "   str(y))

# first window called frame1
frame1 = tk.Frame(root, width=500, height=500, bg=bg)
frame2 = tk.Frame(root, bg=bg)
# frame1.grid(row=0, column=0)
#instead of coping the line 
#instead we will create a for loop 
for fram in (frame1, frame2):
    fram.grid(row=0, column=0, sticky="nesw")

load_frame1()


# run app
root.mainloop()

`

i tried place function and all pack function option , i dtried to use grid on both label and entry but its used in the frame or windows so giving me error

i want to position the entry after the label at the right not in the bottom like this

name of the product [the input filed] name of the product [the input filed]

CodePudding user response:

You can put those labels and entries in a new frame using .grid() and then pack this frame into frame2:

def load_frame2():
    ...
    input_frame = tk.Frame(frame2, bg="#155361", padx=5, pady=5)
    input_frame.pack()
    for i, name in enumerate(all_names):
        tk.Label(
            input_frame,
            text=name,
            bg="#155361",
            fg="white",
            font=("TKMenuFont", 10),
        ).grid(row=i, column=0, sticky='ew')
        tk.Entry(
            input_frame,
            width=10,
        ).grid(row=i, column=1)
    ...
  • Related