python - How to select specific entry widgets for function calls in tkinter using a touch screen? -
i creating touchscreen gui multiple entry widgets edited using keypad. how can know entry widget user selected? i've created roundabout way each entry coded button , when clicked, button edits 'selected' variable keypad uses know edit there better way? possibly using focus? (i have button widgets working want use commented entry widget, irrelevant code has been deleted post wasn't overly long)
class myapp(tk): def __init__(self,*args, **kwargs): tk.__init__(self, *args, **kwargs) container = frame(self) container.pack(side="top", fill="both", expand = true) self.shared_data = { 'codeone' : stringvar(), 'codetwo' : stringvar(), 'selectedcode' : stringvar(), } def setcode(self, value): #function updates selected variable selectedvar = self.shared_data['selectedcode'] if selectedvar.get() == 'codeone': code = self.shared_data['codeone'] elif selectedvar.get() == 'codetwo': code = self.shared_data['codetwo'] else: #non selected print('nothing selected') return false old = code.get() if type(value) == int: code.set(old+str(value)) else: code.set(old[0:len(old)-1]) return true def setvariable(self, variable, value): variable.set(value) return true class menupage(frame): def __init__(self, parent, controller): frame.__init__(self, parent) self.controller = controller label(self, text="code 1:", font='helvetica 15').grid(row=2, column=0, columnspan=3, sticky=e, pady=10) button(self, textvariable=controller.shared_data['codeone'], font=menu_entries, width=7, bg='grey99', command=lambda:controller.setvariable(controller.shared_data['selectedcode'],'codeone')).grid(row=2, column=1, pady=10) #entry(self, textvariable=controller.shared_data['codeone'], font='helvetica 15').grid(row=2, column=3, columnspan=1, pady=10, sticky=w) label(self, text="code 2:", font='helvetica 15').grid(row=3, column=0, columnspan=3, sticky=e, pady=10) button(self, textvariable=controller.shared_data['codetwo'], font=menu_entries, width=7, bg='grey99', command=lambda:controller.setvariable(controller.shared_data['selectedcode'],'codetwo')).grid(row=2, column=1, pady=10) #entry(self, textvariable=controller.shared_data['codetwo'], font='helvetica 15').grid(row=3, column=3, columnspan=3, pady=10, sticky=w) button(self, text="1", width=3, command=lambda:controller.setcode(1)).grid(row=1, column=3, rowspan=2, padx=(20,5), pady=10) button(self, text="2", width=3, command=lambda:controller.setcode(2)).grid(row=1, column=4, rowspan=2, padx=5, pady=10) button(self, text="3", width=3, command=lambda:controller.setcode(3)).grid(row=1, column=5, rowspan=2, padx=5, pady=10) button(self, text="4", width=3, command=lambda:controller.setcode(4)).grid(row=3, column=3, rowspan=2, padx=(20,5), pady=10) button(self, text="5", width=3, command=lambda:controller.setcode(5)).grid(row=3, column=4, rowspan=2, padx=5, pady=10) button(self, text="6", width=3, command=lambda:controller.setcode(6)).grid(row=3, column=5, rowspan=2, padx=5, pady=10) button(self, text="7", width=3, command=lambda:controller.setcode(7)).grid(row=5, column=3, rowspan=2, padx=(20,5), pady=10) button(self, text="8", width=3, command=lambda:controller.setcode(8)).grid(row=5, column=4, rowspan=2, padx=5, pady=10) button(self, text="9", width=3, command=lambda:controller.setcode(9)).grid(row=5, column=5, rowspan=2, padx=5, pady=10) button(self, text="delete", width=7, command=lambda:controller.setcode('delete')).grid(row=7, column=3, rowspan=2, columnspan=2, padx=(20,5), pady=10) button(self, text="0", width=3, command=lambda:controller.setcode(0)).grid(row=7, column=5, rowspan=2, padx=5, pady=10) app = myapp() app.mainloop()
i imagine work same if had traditional mouse , keyboard. when user touches entry widget, widget gets focus. function needs input text widget focus. sort of problem concept of "focus" exists.
delete buttons, put entry widgets back, , change setcode
this:
def setcode(self, value): # widget focus widget = self.focus_get() # insert value widget.insert("insert", value)
you might want give first entry widget focus. need keep reference (it's best practice separate creation of widget adding window grid
, pack
or place
if don't need reference):
entry = entry(self, textvariable=controller.shared_data['codeone'], width=7, bg='grey99') entry.grid(row=2, column=1, pady=10) entry.focus_set()
Comments
Post a Comment