0

Sorry for these easy question, i'm a beginner. I'm trying to get information (name,salary and age for employees) and display it in a list with header (Name,Salary,Age) in window. But I don't know how get information and manage it in a list.

from tkinter import *
from tkinter.ttk import *


    addemploye = Tk()
    addemploye.geometry("400x400")
    Label(addemploye, text="Name").pack()
    e1 = Entry(addemploye, width=20).pack()

    Label(addemploye, text="Salary").pack()
    e2 = Entry(addemploye, width=20).pack()

    Label(addemploye, text="Age").pack()
    e3 = Entry(addemploye, width=20).pack()

    B2 = Button(addemploye , text = "Save", command = getting)
    B2.pack()
    B3 = Button(addemploye, text="Close", command=addemploye.destroy)
    B3.pack()

window = Tk()
window.title("Table with add, edit and delete")
window.geometry('400x400')
window.title("Table with add employee")
window.geometry('500x500')
btn = Button(window, text="+ add new employee",command = addemployee)
btn.place(relx=0.95, rely=0.9, anchor=SE)
window.mainloop()
asal
  • 5
  • 2

1 Answers1

1

A couple of things: Don't create more than one instance of Tk(). See Why are multiple instances of Tk discouraged?. Instead create new windows as Toplevel().

The Button() command parameter expects a function, so I wrote a function which creates the dialog window with Toplevel().

When you call pack() on a widget, the return value will be from pack() and not from the widget creation. Example below where the variable e2 will get the value None.

e2 = Entry(addemploye, width=20).pack()

Instead create the widget first and then pack it:

e2 = Entry(addemploye, width=20)
e2.pack()

The variable e2 will now point to the Entry. You can also manage text from an entry by associating each Entry() with a StringVar().

I have made an example in which I append the data from the entrys to a list. Then you can save this list to wherever you want.

from tkinter import *
from tkinter.ttk import *

window = Tk()
window.geometry('500x500+900+50')
window.title("Table with add employee")

table_row = []  # List to hold data from addemployee

def addemployee():
    dialog = Toplevel(window)
    dialog.geometry('400x400+800+250')
    dialog.title('Add new employee')
    dialog.focus_set()

    Label(dialog, text="Name").pack()
    e1 = Entry(dialog, width=20)
    e1.pack()

    Label(dialog, text="Salary").pack()
    e2 = Entry(dialog, width=20)
    e2.pack()

    Label(dialog, text="Age").pack()
    e3 = Entry(dialog, width=20)
    e3.pack()

    def getting():
        table_row.append(e1.get())  # Append Name to table_row
        table_row.append(e2.get())  # etc.
        table_row.append(e3.get())
        # Save table_row to where you want it
        print(table_row)    # For debugging

    B2 = Button(dialog , text = "Save", command=getting)
    B2.pack()
    B3 = Button(dialog, text="Close", command=dialog.destroy)
    B3.pack()

btn = Button(window, text="+ add new employee", command=addemployee)
btn.place(relx=0.95, rely=0.9, anchor=SE)

window.mainloop()
figbeam
  • 7,001
  • 2
  • 12
  • 18