Home > other >  how to create a label in Tkinter thru an iterable?
how to create a label in Tkinter thru an iterable?

Time:06-15

The idea is to print in labels some output variables thru an iterable. It only created one label and is not the expected result.

I tried by creating the labels inside the Cost() function to display results just as the print does it in the terminal assuming every time the function is called the label is created.


import numpy as np

from tkinter import *

# configure workspace
root = Tk()
root.title("Print cost")
root.geometry('400x400')
root.configure(bg="#202529")


sizes = ['10x15', '13x18', '15x20', '20x25', '20x30', '30x40']

# depending on the volume of prints the price change, last one is a reference price.
T_10x15_cost = [1000, 700, 600, 600]
T_13x18_cost = [120, 100, 80, 80]
T_15x20_cost = [300, 250, 100, 90]
T_20x25_cost = [1000, 800, 250, 250]
T_20x30_cost = [1200, 1000, 300, 250]
T_30x40_cost = [1500, 1200, 400, 600]

cT = (T_10x15_cost, T_13x18_cost, T_15x20_cost, T_20x25_cost, T_20x30_cost, T_30x40_cost)


# Order per size entry

left_frame = Frame (root, bg="#32383D", bd=2, padx=15, pady=5)
left_frame.place(x=10, y=10)

Label(left_frame, text='10x15').grid(row=1, column=0,padx=5, pady=5)
Label(left_frame, text='13x18').grid(row=2, column=0,padx=5, pady=5)
Label(left_frame, text='15x20').grid(row=3, column=0,padx=5, pady=5)
Label(left_frame, text='20x25').grid(row=4, column=0,padx=5, pady=5)
Label(left_frame, text='20x30').grid(row=5, column=0,padx=5, pady=5)
Label(left_frame, text='30x40').grid(row=6, column=0,padx=5, pady=5)

T_10x15 = IntVar(left_frame, value=0)
T_10x15_box = Entry(left_frame, textvariable=T_10x15, width=5)

T_13x18 = IntVar(left_frame, value=0)
T_13x18_box = Entry(left_frame, textvariable=T_13x18, width=5)

T_15x20 = IntVar(left_frame, value=0)
T_15x20_box = Entry(left_frame, textvariable=T_15x20, width=5)

T_20x25 = IntVar(left_frame, value=0)
T_20x25_box = Entry(left_frame, textvariable=T_20x25, width=5)

T_20x30 = IntVar(left_frame, value=0)
T_20x30_box = Entry(left_frame, textvariable=T_20x30, width=5)

T_30x40 = IntVar(left_frame, value=0)
T_30x40_box = Entry(left_frame, textvariable=T_30x40, width=5)

T_10x15_box.grid(row=1, column=1)
T_13x18_box.grid(row=2, column=1)
T_15x20_box.grid(row=3, column=1)
T_20x25_box.grid(row=4, column=1)
T_20x30_box.grid(row=5, column=1)
T_30x40_box.grid(row=6, column=1)


right_frame = Frame (root, bg="#32383D", bd=2, padx=15, pady=5)
right_frame.place(x=200, y=50)

# cost base on volume for each size

def runCost():

    t_10x15 = int(T_10x15_box.get())
    t_13x18 = int(T_13x18_box.get())
    t_15x20 = int(T_15x20_box.get())
    t_20x25 = int(T_20x25_box.get())
    t_20x30 = int(T_20x30_box.get())
    t_30x40 = int(T_30x40_box.get())

    order = [t_10x15, t_13x18, t_15x20, t_20x25, t_20x30, t_30x40]


    def cost():

        prints = order[i]
        if prints <= 50:
            pricePerVolume = 0
        elif 51 <= prints < 101:
            pricePerVolume = 1
        else:
            pricePerVolume = 2
        cost_ip = prints * price[pricePerVolume]

        #original script without GUI. Leaving this print to check funtionallity
        print(prints, ' --- ', size, 'at store = $', cost_ip, '/// REFprice = $' ,prints * price[3])


        #LABEL CREATION ATTEMP #####

        # 1 option
        # costoIP_label = Label(root, textvariable=cost_ip)

        # 2 option
        costIP_label = Label(left_frame, text=' cost: '   str(cost_ip))

        #placement for any of the option
        costIP_label.grid(row=1, column=3,padx=5, pady=5)


    for i in range(len(order)):
        order[i]
        size = sizes[i]
        price = np.array(cT[i])

        cost()


def clearButton():
    T_10x15_box.delete(0, END)
    T_13x18_box.delete(0, END)
    T_15x20_box.delete(0, END)
    T_20x25_box.delete(0, END)
    T_20x30_box.delete(0, END)
    T_30x40_box.delete(0, END)

def close():
    root.destroy()


bottomFrame = Frame(root, bg="#4C555C", bd=2, padx=5, pady=5)
bottomFrame.place(x=10, y=200)

runCostButton = Button(bottomFrame, text='run', command=runCost)
runCostButton.grid(row=8,column=0, padx=10, pady=10)

clearButton = Button(bottomFrame, text='clear', command=clearButton)
clearButton.grid(row=9,column=0, padx=10, pady=10)

CloseButton = Button(bottomFrame, text="exit", command=close)
CloseButton.grid(row=10,column=0, padx=10, pady=10)


root.mainloop()

CodePudding user response:

  1. Don't create the costIP_label in the cost() function, create it beforehand, after the Entry fields, and change only the value in the "text" field using textvariable. If you want to make it invisible at the beginning, then hide it with the grid_forget() command or put an empty string "" in it.

  2. You started well by creating the sizes list, but if you are not going to change it in the program, then of course a tuple is better.

  3. All further objects depend on it, and if you create new objects based on it, then in the future, if it changes, you will not need to go into the program and change various variables like T_10x15_cost, T_10x15, T_10x15_box.

    Let's say cT, a list of lists, can be converted to a dictionary where the keys will be from sizes, and the values ​​from price lists. cT = {sizes[0] : [1000, 700, 600, 600] ...

  4. It is better to place elements in a cycle, and not repeat almost the same thing:

for i in range(len(sizes)):
    Label(left_frame, text=sizes[i]).grid(row=i 1, column=0, padx=5, pady=5)

I will be glad if my notes help you somehow.

  • Related