Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Treeview Tkinter widget - clickable links

Tags:

I have ten links in my treeview widget (http://www.example.com, http://www.example1.com and so on). It is plain text inserted into treeview. Is it possible to make it clickable? How can I convert text into links? Is it possible inside treeview widget?

This is a part of my treeview: treeview

I would like to make those lines clickable links (like in normal browser). Simply click, open default browser and go to page (http://dieta.pl/ for example).

Here is example of my code (a part):

# !/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from google import search
from urlparse import urlparse
from SiteCrawler import SiteCrawler
import Tkinter as tk
from Tkinter import *
import ttk
# from Tkinter.font import Font


class Main(Frame):

def __init__(self):
    self.fraza = None
    self.master = tk.Tk()
    if os.name == 'nt':
        self.master.state('zoomed')
    else:
        self.master.wm_attributes('-zoomed', 1)
    self.master.title('Site crawler')
    self.master.geometry("800x600+600+200")

    # Frame głowny
    self.f = Frame(self.master)
    self.f.place(relx=.5, rely=.35, anchor="c")

    # Label do wpisania frazy
    L1 = Label(self.master, text=u"Wpisz frazę", font="Verdana 15 bold")
    L1.grid(in_=self.f, row=1, column=2)

    # Entry box do wpisania frazy
    self.phrase = Entry(self.master, font="Verdana 15 bold",
                        justify=CENTER)
    self.phrase.grid(in_=self.f, row=1, column=3, columnspan=3)

    # Button do zatwierdzenia frazy
    Bt1 = Button(self.master, text=u'Wczytaj frazę',
                 command=lambda: self.results(self.phrase.get()), width=20)
    Bt1.grid(in_=self.f, row=2, column=3, columnspan=3)

    # ttk.tree widget
    tree_cols = ('Lp', 'Url', 'Fraza w Title',
                 'Fraza w description', 'Fraza w Keywords',
                 'Fraza w H1', 'Fraza w H2', 'Fraza w całej stronie')
    self.tree = ttk.Treeview(columns=tree_cols,
                             show='headings', height=10)

    for i in tree_cols:
        self.tree.heading(i, text=i)

    self.tree.column('Lp', width=50, anchor=CENTER)
    # self.tree.heading("two", text="Fraza w Title")
    # self.tree.heading("three", text="Fraza w Description")
    # self.tree.heading("four", text="Fraza w Description")

    self.tree.grid(in_=self.f, row=4, column=1, columnspan=4, sticky=NSEW)

    self.master.mainloop()

def results(self, phrase):
    Crawler = SiteCrawler()
    self.fraza = phrase

    domains = []
    for i in search(phrase, stop=10):
        print i
        parsed_url = urlparse(i)
        domain = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_url)
        if domain not in domains:
            domains.append(domain)

    for index, url in enumerate(domains[:10]):
        h = ['h1', 'h2']
        Crawler.load_url(url, self.fraza)
        Crawler.title()
        Crawler.get_description()
        Crawler.get_keywords()
        for i in h:
            Crawler.count_H(i)
        Crawler.all_keywords()

        self.tree.insert('', 'end', values=(
            index + 1, url, Crawler.title(), Crawler.get_description(),
            Crawler.get_keywords(), Crawler.count_H('h1'),
            Crawler.count_H('h2'), Crawler.all_keywords()))

if __name__ == "__main__":
    main = Main()
    main.results()
like image 910
jundymek Avatar asked Jul 29 '16 19:07

jundymek


1 Answers

For each widget, you can bind Python functions and methods to events.Bind a function to your treeview.You need to bind your tree,add this in __init__ function:

self.tree.bind("<Double-1>", self.link_tree)

Create a function:

def link_tree(self,event):
    input_id = self.tree.selection()
    self.input_item = self.tree.item(input_id,"text")

    #for opening the link in browser
    import webbrowser
    webbrowser.open('{}'.format(self.input_item))
    #do whatever you want
like image 184
shivsn Avatar answered Sep 28 '22 02:09

shivsn