I'm learning to code a program with GUI, but I can't get my head around with the best principle.
At the moment I'm trying to have 5 entries and the text which would be written to entries would automatically update to labels. Here is my code so far:
import tkinter as tk
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
# GUI starts here
self.label = tk.Label(parent, text="Give values")
self.label.grid(row="1")
self.entries()
self.labels()
def entries(self):
for i in range(5):
number1 = tk.StringVar()
entry = tk.Entry(textvariable=number1)
entry.grid(row=3, column=i)
result = entry.get()
return result
def labels(self,):
for i in range(5):
label = tk.Label(self.parent, text=self.entries(), width=17, borderwidth=2, relief="groove")
label.grid(row=4, column=i)
if __name__ == "__main__":
root = tk.Tk()
root.geometry("1280x800")
MainApplication(root).grid()
root.mainloop()
The output of my code is following. Apparently, lots of things are wrong because I don't get five entry boxes and they don't update to labels below automatically.
I have two questions:
- How to fix my code to get wanted output
- Would it be a better way to build the GUI using nested classes inside my MainApplication class instead of methods.