I created a tkinter application, in which I added my personal Form functionality. The purpose of the Form class is to allow a user of the class to create tkinter forms quickly by providing it with either a question or reactive question. For the purpose of keeping things short, I will be shortening the code and will not show the whole functionality of the classes.The functionality I'm struggling with is the binding of the info field (A text label) to a function which will open the browser in the version and settings tab. I don't understand why the binding is not working, I've used several examples in which people had a similar code work for them, but this won't even print “click” to the console.
Here is the instance code, the class will be provided bellow this section.
The validation function is a callback function that the user can create in which he iterates through all the values from the form. The form is created with 2 questions, each of which has a url that will redirect them to their version and settings tab. (True in the validation function means there was an error found)
def validate(values)->bool: for _,value in values.items(): if value =="": return True,"No se aceptan valores vacios" return False,None form = Form("Configuraciones",[ Question("text","Ruta de Perfil",self.configuraciones[0],info="Se encuentra en\n chrome://version \n Copia el valor de Ruta del perfil\n Solo copia hasta User Data",url="chrome://version"), Question("text","Folder fuente",self.configuraciones[1],info="Se encuentra en\n chrome://settings/downloads",url="www.google.com") ],validate) values,_ = form.run()
The Question class is a simple object describing the question, nothing much to see here
class Question:def __init__(self,type,label,default="",choices:List[str]=[],info:str="",main=False,url=""): self.type = type self.label=label self.default = default self.choices=choices self.info=info self.main=main self.url = url
Then the Form class is where most of the logic happens, here a new tkinter window is created and numerous variables are created, of which values and questions are the most important ones. Values will store the inputed values by the user. Then a generating function will be run, in which based on the type of question a different function will be executed, for this case we only need to focus on the text kind. I'll omit the code of the other kinds. As the issue is in create_text_input
class Form: def __init__(self,title,questions:List[Question|ReactiveQuestion],validation_cb,msg=""): self.main=None self.window = tk.Tk() self.window.wm_title(title) self.questions = questions self.reactive = False self.validation_cb = validation_cb for i in self.questions: self.generator(i) def generator(self,question:Question|ReactiveQuestion): match question.type: case "text": self.create_text_input(question.label,question.default,question.info,question.url) case "select": self.create_select_input(question.label,question.choices)
When looking at the text input generating function info is checked to assure it's not an empty string, if it's not then the information label will be shown and the text label will be subjected to the addActionPerformed function. Where the text label will be binding to clicking the mouse button. The issue is that not even the print("click") is running. I don't even get an error message.
def addActionPerformed(self,source,url): def open_url(): print("click") webbrowser.open_new_tab(url) source.bind("<Button-1>",lambda event: open_url)
Note: addActionPerformed is also a method of the Form class (indenting is hard in this editor)
def create_text_input(self,label,default,info,url): tk.Label(self.window,text=label).pack(padx=6,pady=3) if info != "": text_label = tk.Label(self.window, text=info) text_label.pack() self.addActionPerformed(text_label, url) value = tk.StringVar(self.window,value=default) tk.Entry(self.window, textvariable=value).pack(padx=6,pady=3) self.values[label]=value