Python tkinter simplification

145 views Asked by At

Is there any way I can write this, with a function or anything similar, so that it doesn't take up so much room?

    self.birth_date_ent = Entry(self)
    self.birth_date_ent.grid(row = 0, column = 1, sticky = W)
    self.birth_date_ent.insert(0, "YYYY-MM-DD")

    self.birth_time_ent = Entry(self)
    self.birth_time_ent.grid(row = 1, column = 1, sticky = W)
    self.birth_time_ent.insert(0, "HR:MM") 

    self.partner_sign_ent = Entry(self)
    self.partner_sign_ent.grid(row = 2, column = 1, sticky = W)
    self.partner_sign_ent.insert(0, "YYYY-MM-DD")

Thanks!

1

There are 1 answers

0
poke On

Well, to make it a bit more DRY, you could write yourself a function:

def createEntry (ref, row, column, format):
    e = Entry(ref)
    e.grid(row=row, column=column, sticky=W)
    e.insert(0, format)
    return e

self.birth_date_ent = createEntry(self, 0, 1, "YYYY-MM-DD")
self.birth_time_ent = createEntry(self, 1, 1, "HR:MM") 
self.partner_sign_ent = createEntry(self, 2, 1, "YYYY-MM-DD")