from tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
frtop=self.winfo_toplevel() #Flexible Toplevel of the window
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
print("width="+str(screen_width)+", height="+str(screen_height))
max_windows_size=str(screen_width)+"x"+str(screen_height)
frtop.geometry(max_windows_size)
self.grid()
self.cash_frame = Frame(self)
self.cash_frame.grid(column =0, row =0, sticky='NSEW',columnspan=1)
self.cash_lbl = Label(self.cash_frame, text = "Cash:", font=('Arial',20,'bold'))
self.cash_lbl.grid(column =0, row =0)
self.cash_str_null = StringVar()
self.cash_str_null.set('')
self.cash = Entry(self.cash_frame, width=5,textvariable=self.cash_str_null, font=('Arial',26,'bold'))
self.cash.grid(column =1, row =0)
self.exchange_frame = Frame(self)
self.exchange_frame.grid(column =0, row =1, sticky='NSEW')
self.lbl_exchange = Label(self.exchange_frame, text = "exchange",font=('Arial',20,'bold'))
self.lbl_exchange.grid(column =0, row =0)
self.exchange_str_null = StringVar()
self.exchange_str_null.set('')
self.exchange_value = Entry(self.exchange_frame, width=5,textvariable=self.exchange_str_null,font=('Arial',20,'bold'))
self.exchange_value.grid(column =1, row =0)
self.add_one = Button(self, text = "1" , fg = "red", command=self.add_one_in_entry,font=('Arial',20,'bold'))
self.add_one.grid(column=0, row=2)
def add_one_in_entry(self):
pass
if __name__ == "__main__":
app = Application()
app.master.title("Sample application")
app.mainloop()
How can I do that ?
Thanks
PS : I added answer as per my knowledge . I would still appreciate if you know another way or have suggestions about code improvement .
First , I added name attribute to entry so that print shows my customized name instead of . or .!application.!frame2.!entry or .!application.!frame.!entry.
I used focus_get to get widget after verifying its type
Here is my updated code
from tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
frtop=self.winfo_toplevel() #Flexible Toplevel of the window
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
print("width="+str(screen_width)+", height="+str(screen_height))
max_windows_size=str(screen_width)+"x"+str(screen_height)
frtop.geometry(max_windows_size)
self.grid()
self.cash_frame = Frame(self)
self.cash_frame.grid(column =0, row =0, sticky='NSEW',columnspan=1)
self.cash_lbl = Label(self.cash_frame, text = "Cash:", font=('Arial',20,'bold'))
self.cash_lbl.grid(column =0, row =0)
self.cash_str_null = StringVar()
self.cash_str_null.set('')
self.cash = Entry(self.cash_frame, width=5,textvariable=self.cash_str_null, font=('Arial',26,'bold'))
self.cash.name = "cash_entry"
self.cash.grid(column =1, row =0)
self.exchange_frame = Frame(self)
self.exchange_frame.grid(column =0, row =1, sticky='NSEW')
self.lbl_exchange = Label(self.exchange_frame, text = "exchange",font=('Arial',20,'bold'))
self.lbl_exchange.grid(column =0, row =0)
self.exchange_str_null = StringVar()
self.exchange_str_null.set('')
self.exchange_value = Entry(self.exchange_frame, width=5,textvariable=self.exchange_str_null,font=('Arial',20,'bold'))
self.exchange_value.name = "exchange_entry"
self.exchange_value.grid(column =1, row =0)
self.add_one = Button(self, text = "1" , fg = "red", command=self.add_one_in_entry,font=('Arial',20,'bold'))
self.add_one.grid(column=0, row=2)
def add_one_in_entry(self):
focussed_widget=app.focus_get()
#if(str(app.focus_get().__class__)=="<class 'tkinter.Entry'>"):
if isinstance(focussed_widget, Entry):
print(app.focus_get().name)
focussed_widget.insert(0,'1')
else:
print('no text entry selected')
pass
if __name__ == "__main__":
global app
app = Application()
app.master.title("Sample application")
app.mainloop()