0

It basically just only make a button for one URL (https://github.com/explore) when it has all the other links on the github homepage to open. It should make a button for the individual links.

        webbrowser.open(url)

    def convert_to_tk(self, parent):   
        converted_html = []
        tk_elements = {}
 
        for x in self.tags:
            tk_elements[x] = []

        for x in self.filtered_html:
            if not f"<{x['tag']}" in x["value"] and not "<" in x["value"] and ((x["value"] != "" and x["value"] != "'") or x["attrs"] != {}):
                converted_html.append({
                    "tag": x["tag"],
                    "attrs": x["attrs"],
                    'value': x["value"]
                })
        
        for x in converted_html:
            tag = x["tag"]
            value = x["value"]
            attrs = x["attrs"]

            if "href" in attrs:
                href = urljoin(self.url, attrs["href"])
            
            if tag == "h1":
                tk_elements["h1"].append(Label(parent, text=value, font=("Arial", 60)))
            elif tag == "h2":
              tk_elements["h2"].append(Label(parent, text=value, font=("Arial", 50)))
            elif tag == "h3":
              tk_elements["h3"].append(Label(parent, text=value, font=("Arial", 40)))
            elif tag == "h4":
              tk_elements["h4"].append(Label(parent, text=value, font=("Arial", 30)))
            elif tag == "h5":
              tk_elements["h5"].append(Label(parent, text=value, font=("Arial", 20)))
            elif tag == "h6":
              tk_elements["h6"].append(Label(parent, text=value, font=("Arial", 10)))
            elif tag == "a":
                tk_elements["a"].append(Button(parent, text=value, command=lambda: os.system(f"start \"\" {href}")))
                # print(f"Used the link: {href}")

        return tk_elements
j_4321
  • 15,431
  • 3
  • 34
  • 61
  • Does this answer your question? [tkinter creating buttons in for loop passing command arguments](https://stackoverflow.com/questions/10865116/tkinter-creating-buttons-in-for-loop-passing-command-arguments) – j_4321 Jul 18 '22 at 07:40

1 Answers1

0

This should do it. I added href=href to your lambda function. It sets each lambda with the corresponding value. Otherwise, you'll just get the same link for all your buttons.

tk_elements["a"].append(Button(parent, text=value, command=lambda href=href: os.system(f"start \"\" {href}")))
dem0ur3r
  • 1
  • 2