Question:
I have 2 similar tkinter windows, which I would like to condense or modularise (because i have pretty much 2 of the exact same function), which would make the code more compact and more easily readable (whats the point of having 2 functions if i could have just one?).
How would i do this and should i use a class or a function?
Code:
from tkinter import *
import sys
def register_normal():
window1=Tk()
window1.title('Registration')
window1.geometry('300x300')
def register(*args):
print('This works aswell!')
label_hidden=Label(window1, text="")
photo1=PhotoImage(file='registration.png')
photo_label1=Label(window1, image=photo1)
label_hidden_1=Label(window1,text="")
new_username_label=Label(window1,text='NEW USERNAME',font=('arial', 10, 'bold'))
login_username=Entry(window1, width=40)
label_hidden_2=Label(window1,text="")
new_password_label=Label(window1,text='NEW MASTER PASSWORD',font=('arial', 10, 'bold'))
login_password=Entry(window1,width=40, show='*')
label_hidden_3=Label(window1,text="")
login_btn=Button(window1,text='REGISTER',width=20,height=1,font=('arial', 10, 'bold'), command=register)
login_password.bind('<Return>', register)
label_hidden_1.pack()
photo_label1.pack()
label_hidden.pack()
new_username_label.pack()
login_username.pack()
label_hidden_2.pack()
new_password_label.pack()
login_password.pack()
label_hidden_3.pack()
login_btn.pack()
window1.mainloop()
def login_normal():
window2=Tk()
window2.title('Email Manager')
window2.geometry('300x300')
def login(*args):
print('This works!')
label_hidden=Label(window2, text="")
photo2=PhotoImage(file='email.png')
photo_label2=Label(window2, image=photo2)
label_hidden_1=Label(window2,text="")
username_lable=Label(window2,text='USERNAME',font=('arial', 10, 'bold'))
login_username=Entry(window2, width=40)
label_hidden_2=Label(window2,text="")
password_lable=Label(window2,text='MASTER PASSWORD',font=('arial', 10, 'bold'))
login_password=Entry(window2,width=40, show='*')
label_hidden_3=Label(window2,text="")
login_btn=Button(window2,text='LOGIN',width=20,height=1,font=('arial', 10, 'bold'), command=login)
login_password.bind('<Return>', login)
label_hidden_1.pack()
photo_label2.pack()
label_hidden.pack()
username_lable.pack()
login_username.pack()
label_hidden_2.pack()
password_lable.pack()
login_password.pack()
label_hidden_3.pack()
login_btn.pack()
window2.mainloop()
def main():
register_normal()
login_normal()
if __name__ == '__main__':
main()
#===================================icons-and-pictures-author-attribute======================================#
#===========Icons made by "https://www.freepik.com" "Freepik" from "https://www.flaticon.com/" ==============#
#===================================icons-and-pictures-author-attribute======================================#