0

I want to programm a simple 'calculator'.. Some Input fields, then some calculations with those inputs in the background and then I want to see output.

In a simple form it looks like that:

enter image description here

a-e: are input fields.

f-h: output fields.

The calculations in the background and the showing of output goes well.

Now I want to have a line between e and f that says: Output

Without any entry field or sth.. just a textfield. Can someone help me, how to insert a textfield on a specific place ? This is the code:

fields = ('a', 'b', 'c', 'd', 'e',
          'f (in EUR)', 'g (days per year)', 'h in %')

def initUI(self):

    self.master.title("Calculator")
    self.pack(fill=BOTH, expand=1)

    Style().configure("TFrame", background="#111")


    bardejov = ImageTk.PhotoImage(bard)
    label1 = Label(self, image=bardejov)
    label1.image = bardejov
    label1.place(x=20, y=20)

def total_assets(entries):
    # period rate:
    a = (float(entries['a'].get()))
    b = (float(entries['b'].get()))
    c =  (float(entries['c'].get()))
    d = (float(entries['d'].get()))
    e = (float(entries['e'].get()))

entries['f'].insert(0, calc1 )
    entries['g'].insert(0, np.round( calc2 ) )
    entries['h'].insert(0, calc3 )
#calc1,cal2,calc3 are some calculations with the input fields

def makeform(root, fields):
    entries = {}
    for field in fields:
        print(field)
        row = tk.Frame(root)
        lab = tk.Label(row, width=50, text=field+": ", anchor='w')
        ent = tk.Entry(row)
        ent.insert(0, "0")
        row.pack(side=tk.TOP, 
                 fill=tk.X, 
                 padx=5, 
                 pady=5)
        lab.pack(side=tk.LEFT)
        ent.pack(side=tk.RIGHT, 
                 expand=tk.YES, 
                 fill=tk.X)
        entries[field] = ent
        ent.config(background="gray")
    return entries


if __name__ == '__main__':

    root = tk.Tk()
    root.title("hello")
    root.columnconfigure(1, weight=1)
    #root.geometry("1000x300") #Width x Height
    #root.grid_columnconfigure("50")

    ents = makeform(root, fields)
    b1 = tk.Button(root, text='do',
           command=(lambda e=ents: total_assets(e)))
    #ents.place(x=10, y=115, height=30, width=200)
    b1.pack(side=tk.LEFT, padx=5, pady=5)
    b2 = tk.Button(root, text = "reset",
                   command=(lambda e=ents: restart_program(e)))
    b2.pack(side=tk.LEFT, padx=5, pady=5)
    root.mainloop()

1 Answer 1

2

You can use below code to create a separator with text:

def create_separator(parent, text=None):
    frame = tk.Frame(parent)
    frame.columnconfigure(0, weight=1)
    ttk.Separator(frame, orient=tk.HORIZONTAL).grid(row=0, column=0, sticky='ew')
    if text:
        tk.Label(frame, text=text).grid(row=0, column=0, padx=10, sticky='w')
    return frame

Then inside the for loop of makeform():

for field in fields:
    ...
    ent.config(background="gray")
    if field == 'e':
        # add a separator
        sep = create_separator(root, 'Output')
        sep.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.