How to open a link after selecting an option from a list box?

Viewed 63

I am trying to make it so when I select an option from my drop-down list and click ok it should open up a tab with the desired link.

Here is my code:

from tkinter import * 
import tkinter as tk
import webbrowser



app = tk.Tk()


#Gets the selected item in list. Displays number in list
def ok ():
    print (lb.curselection ())
    #if  lb.curselection == 0 webbrowser.open('https://www.win-rar.com/predownload.html?&L=0')
    #if  lb.curselection == 1 webbrowser.open('https://www.google.com/chrome/')
    #if  lb.curselection == 2 webbrowser.open('https://sourceforge.net/projects/openofficeorg.mirror/files/4.1.8/binaries/en-US/Apache_OpenOffice_4.1.8_Win_x86_install_en-US.exe/download')
    #if  lb.curselection == 3 webbrowser.open('https://get.adobe.com/uk/reader/download/?installer=Reader_DC_2020.013.20064_English_UK_for_Windows&os=Windows%2010&browser_type=KHTML&browser_dist=Chrome&dualoffer=false&mdualoffer=true&cr=false&stype=7775&d=McAfee_Security_Scan_Plus&d=McAfee_Safe_Connect')
    #if  lb.curselection == 4 webbrowser.open('https://download.mozilla.org/?product=thunderbird-78.5.1-SSL&os=win&lang=en-US')

#listbox 
def OpenList():
    global lb
    lb = Listbox (app)
    lb.insert (1, "WinRar")
    lb.insert (2, "Google Chrome")
    lb.insert (3, "OpenOffice")
    lb.insert (4, "Adobe Reader")
    lb.insert (5, "Thunderbird")
    lb.grid (row = 3, column = 3)
    but = Button (app, text="OK", command=ok)
    but.grid ( row = 4, column = 3)

softwareManagerLabel = Label (app , text="Software Manager")
softwareManagerLabel.grid (row = 0, column = 3)


#Button opens listbox
dropDownButton = tk.Button(app, text="Install a Program From The Network", command=OpenList)
dropDownButton.grid (row = 1, column = 3)

I have commented out the links for now. Any help would be appreciated!

1 Answers

First you missed a (). Then you are calling a tuple, so you need to index to the first item in it, like:

def ok():
    print(lb.curselection()[0])
    if  lb.curselection()[0] == 0: webbrowser.open('https://www.win-rar.com/predownload.html?&L=0')
    if  lb.curselection()[0] == 1: webbrowser.open('https://www.google.com/chrome/')
    if  lb.curselection()[0] == 2: webbrowser.open('https://sourceforge.net/projects/openofficeorg.mirror/files/4.1.8/binaries/en-US/Apache_OpenOffice_4.1.8_Win_x86_install_en-US.exe/download')
    if  lb.curselection()[0] == 3: webbrowser.open('https://get.adobe.com/uk/reader/download/?installer=Reader_DC_2020.013.20064_English_UK_for_Windows&os=Windows%2010&browser_type=KHTML&browser_dist=Chrome&dualoffer=false&mdualoffer=true&cr=false&stype=7775&d=McAfee_Security_Scan_Plus&d=McAfee_Safe_Connect')
    if  lb.curselection()[0] == 4: webbrowser.open('https://download.mozilla.org/?product=thunderbird-78.5.1-SSL&os=win&lang=en-US')

And you missed a : at the end of if statement, and hence the syntax error too.

Another method: Though if I were to reduce and neaten the codes more, I would make a dictionary with the keys as index and the links as values, like:

links = {0: 'https://www.win-rar.com/predownload.html?&L=0', 
    1: 'https://www.google.com/chrome/', 
    2: 'https://sourceforge.net/projects/openofficeorg.mirror/files/4.1.8/binaries/en-US/Apache_OpenOffice_4.1.8_Win_x86_install_en-US.exe/download', 
    3: 'https://get.adobe.com/uk/reader/download/?installer=Reader_DC_2020.013.20064_English_UK_for_Windows&os=Windows%2010&browser_type=KHTML&browser_dist=Chrome&dualoffer=false&mdualoffer=true&cr=false&stype=7775&d=McAfee_Security_Scan_Plus&d=McAfee_Safe_Connect', 
    4: 'https://download.mozilla.org/?product=thunderbird-78.5.1-SSL&os=win&lang=en-US'}

And then I will loop though, the dictionary, checking each iterated item with the indexed number of the selected item from the listbox, like:

# Gets the selected item in list. Displays number in list
def ok():
    for a in links: #loop through dict
        if lb.curselection()[0] == a: #check the indexed num with looped item
            webbrowser.open(links[a]) #if satisfies then open the link
            break #then break the loop

links = {0: 'https://www.win-rar.com/predownload.html?&L=0', 
    1: 'https://www.google.com/chrome/', 
    2: 'https://sourceforge.net/projects/openofficeorg.mirror/files/4.1.8/binaries/en-US/Apache_OpenOffice_4.1.8_Win_x86_install_en-US.exe/download', 
    3: 'https://get.adobe.com/uk/reader/download/?installer=Reader_DC_2020.013.20064_English_UK_for_Windows&os=Windows%2010&browser_type=KHTML&browser_dist=Chrome&dualoffer=false&mdualoffer=true&cr=false&stype=7775&d=McAfee_Security_Scan_Plus&d=McAfee_Safe_Connect', 
    4: 'https://download.mozilla.org/?product=thunderbird-78.5.1-SSL&os=win&lang=en-US'}

The key here is to understand that when your repeating bunch of code just with small changes, there will be a way to reduce repeating yourself, in this case its using a loop. More than 90% times the repetitions can be reduced down.

Either ways, I wouldn't keep such lengthy links in my current script, I would store it in another script and import it from there. But for small projects, this is fine.

Related