0

I have made a list of buttons yesterday, and i wanted to step it up a little by adding a hyperlink for each item in the list. However, pressing each button results in nothing at all (In fact, I believe a 'None' was resulted from this). All the YouTube links were made into an array, and for each item, i have tried to assign a hyperlink.

Here is the code for this:

from tkinter import *
import webbrowser

root = Tk()
root.title('Scrollbar text box')
root.geometry("600x500")




#my exercise list

FullExerciseList = [
    "Abdominal Crunches", 
    "Russian Twist",
    "Mountain Climber",
    "Heel Touch" ,
    "Leg Raises",
    "Plank",
    "Cobra Stretch",
    "Arm Raises",
    "Side Arm Raises",
    "Tricep Dips",
    "Arm Circles Clockwise",
    "Arm Circles Counter Clockwise",
    "Diamond Push Ups",
    "Jumping Jacks" ,
    "Chest Press Pulse",
    "Push Ups" ,
    "Wall Push Ups",
    "Triceps Stretch Left" ,
    "Tricep Stretch Right",
    "Cross Arm Stretch" ,
    "Rhomboid Pulls",
    "Knee Push Ups",
    "Arm Scissors",
    "Cat Cow Pose",
    "Child Pose",
    "Incline Push Ups",
    "Wide Arm Push Ups",
    "Box Push Ups",
    "Hindu Push Ups",
    "Side Hop",
    "Squats",
    "Side Lying Lift Left",
    "Side Lying Lift Right",
    "Backward Lunge",
    "Donkey Kicks Right",
    "Donkey Kick Left",
    "Left Quad Stretch",
    "Right Quad Stretch",
    "Wall Calf Raises"
    ]





#def yview function

def multiple_yview(*args):
    my_text1.yview(*args)
    my_text1.yview(*args)
    



#frame

my_frame = Frame(root)
my_frame.pack(pady=20)



#scrollbar

text_scroll = Scrollbar(my_frame)
text_scroll.pack(side = RIGHT, fill = Y)

# changed yscrollcommand=text_scroll to yscrollcommand=text_scroll.set
my_text1 = Text(my_frame, width=20, height=25, font=("Helvetica", 16), yscrollcommand=text_scroll.set, wrap='none')
my_text1.pack(side=RIGHT, padx=5)

#configuring scroll bar

text_scroll.config(command = multiple_yview)




vid_links = [
    "https://www.youtube.com/watch?v=RUNrHkbP4Pc&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=9", 
    "https://www.youtube.com/watch?v=Lha66p0ZXUc&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=62",
    "https://www.youtube.com/watch?v=Lha66p0ZXUc&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=62",
    "https://www.youtube.com/watch?v=YslHgg2E-Ro&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=95",
    "https://www.youtube.com/watch?v=_LGpDtENZ5U&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=64",
    "https://www.youtube.com/watch?v=dcJVA2sBPqw&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=92",
    "https://www.youtube.com/watch?v=kqnua4rHVVA",
    "https://www.youtube.com/watch?v=Fz4oo1vFo9M&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=81",
    "https://www.youtube.com/watch?v=DMwRPGMPB10&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=75",
    "https://www.youtube.com/watch?v=z21McHHOpAg&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=11"
    "https://www.youtube.com/watch?v=ATusl0jg4SU",
    "https://www.youtube.com/watch?v=UCmqw3kKZ38&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=60"
    "https://www.youtube.com/watch?v=SJ1Xuz9D-ZQ",
    "https://www.youtube.com/watch?v=SJ1Xuz9D-ZQ",
    "https://www.youtube.com/watch?v=9bR-elyolBQ&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=3",
    "https://www.youtube.com/watch?v=HE0ijmUc6Og&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=51",
    "https://www.youtube.com/watch?v=3WUUeM07i_Q&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=42",
    "https://www.youtube.com/watch?v=2W4ZNSwoW_4&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=2",
    "https://www.youtube.com/watch?v=jWxvty2KROs&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=7",
    "https://www.youtube.com/watch?v=DxBDqHL5NlM",
    "https://www.youtube.com/watch?v=dGKbTKLnym4&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=29",
    "https://www.youtube.com/watch?v=wQq3ybaLZeA&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=4",
    "https://www.youtube.com/watch?v=Fcbw82ykBvY&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=15",
    "https://www.youtube.com/watch?v=R08gYyypGto&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=13",
    "https://www.youtube.com/watch?v=DEyDbzSudEU&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=93",
    "https://www.youtube.com/watch?v=DxBDqHL5NlM",
    "https://www.youtube.com/watch?v=DJQGX2J4IVw&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=10",
    "https://www.youtube.com/watch?v=YslHgg2E-Ro&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=95",
    "https://www.youtube.com/watch?v=nYmUEJIBj3c&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=66",
    "https://www.youtube.com/watch?v=jgh6sGwtTwk",
    "https://www.youtube.com/watch?v=jgh6sGwtTwk",
    "https://www.youtube.com/watch?v=42bFodPahBU&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=76",
    "https://www.youtube.com/watch?v=JhX1nBnirNw&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=19",
    "https://www.youtube.com/watch?v=SaZK9vlSmHI",
    "https://www.youtube.com/watch?v=SaZK9vlSmHI",
    "https://www.youtube.com/watch?v=GQa_N7wft7M",
    "https://www.youtube.com/watch?v=EOf3cGIQpA4&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=71",
    "https://www.youtube.com/watch?v=pQUsUHvyoI0&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=43",
    ]

for link in vid_links:
    
    def callback():
        a = 0
        for i in range(len(vid_links)):
            webbrowser.open_new('{}'+'"{}"'.format('r', link[a]))
            a+=1

for item in FullExerciseList:
    button = Button(my_text1, text=item, command = callback) 
    # inserting button into my_text1
    my_text1.window_create('end', window=button)
    # add a newline so that each button is in a separate line
    my_text1.insert('end', '\n')

Any help is greatly appreciated =)

superbo9y
  • 13
  • 3
  • 1. The `callback` function is overwritten with every iteration through the for loop. 2. When I executed your code, `'{}'+'"{}"'.format('r', link[a])` always returned `{}"r"`. So, I think there is a logical error in that line. – Sriram Srinivasan Apr 09 '22 at 10:09
  • do you think i should put both statements in the same function? – superbo9y Apr 09 '22 at 11:03
  • What do you intend to do in this line: `webbrowser.open_new('{}'+'"{}"'.format('r', link[a]))`? Did you face any problem with `webbrowser.open_new(link)`? – Sriram Srinivasan Apr 09 '22 at 11:07
  • I wanted to iterate through the list to assign a link to each button. Using ` webbrowser.open_new(link) ` opened many links at once, my browser was unable to handle! – superbo9y Apr 09 '22 at 14:12

1 Answers1

1

I made the following modifications to your code:

  1. Removed callback and used lambda to open the corresponding YouTube video.
  2. Used zip to pair-wise extract the exercise name and the video link.

If you want to know why I wrote lambda x=link: webbrowser.open_new(x) and not lambda : webbrowser.open_new(link) refer to this question.


Working Code:

from tkinter import *
import webbrowser

root = Tk()
root.title('Scrollbar text box')
root.geometry("600x500")


#my exercise list

FullExerciseList = [
    "Abdominal Crunches", 
    "Russian Twist",
    "Mountain Climber",
    "Heel Touch" ,
    "Leg Raises",
    "Plank",
    "Cobra Stretch",
    "Arm Raises",
    "Side Arm Raises",
    "Tricep Dips",
    "Arm Circles Clockwise",
    "Arm Circles Counter Clockwise",
    "Diamond Push Ups",
    "Jumping Jacks" ,
    "Chest Press Pulse",
    "Push Ups" ,
    "Wall Push Ups",
    "Triceps Stretch Left" ,
    "Tricep Stretch Right",
    "Cross Arm Stretch" ,
    "Rhomboid Pulls",
    "Knee Push Ups",
    "Arm Scissors",
    "Cat Cow Pose",
    "Child Pose",
    "Incline Push Ups",
    "Wide Arm Push Ups",
    "Box Push Ups",
    "Hindu Push Ups",
    "Side Hop",
    "Squats",
    "Side Lying Lift Left",
    "Side Lying Lift Right",
    "Backward Lunge",
    "Donkey Kicks Right",
    "Donkey Kick Left",
    "Left Quad Stretch",
    "Right Quad Stretch",
    "Wall Calf Raises"
    ]





#def yview function

def multiple_yview(*args):
    my_text1.yview(*args)
    my_text1.yview(*args)
    



#frame

my_frame = Frame(root)
my_frame.pack(pady=20)



#scrollbar

text_scroll = Scrollbar(my_frame)
text_scroll.pack(side = RIGHT, fill = Y)

# changed yscrollcommand=text_scroll to yscrollcommand=text_scroll.set
my_text1 = Text(my_frame, width=20, height=25, font=("Helvetica", 16), yscrollcommand=text_scroll.set, wrap='none')
my_text1.pack(side=RIGHT, padx=5)

#configuring scroll bar

text_scroll.config(command = multiple_yview)




vid_links = [
    "https://www.youtube.com/watch?v=RUNrHkbP4Pc&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=9", 
    "https://www.youtube.com/watch?v=Lha66p0ZXUc&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=62",
    "https://www.youtube.com/watch?v=Lha66p0ZXUc&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=62",
    "https://www.youtube.com/watch?v=YslHgg2E-Ro&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=95",
    "https://www.youtube.com/watch?v=_LGpDtENZ5U&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=64",
    "https://www.youtube.com/watch?v=dcJVA2sBPqw&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=92",
    "https://www.youtube.com/watch?v=kqnua4rHVVA",
    "https://www.youtube.com/watch?v=Fz4oo1vFo9M&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=81",
    "https://www.youtube.com/watch?v=DMwRPGMPB10&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=75",
    "https://www.youtube.com/watch?v=z21McHHOpAg&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=11",
    "https://www.youtube.com/watch?v=ATusl0jg4SU",
    "https://www.youtube.com/watch?v=UCmqw3kKZ38&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=60",
    "https://www.youtube.com/watch?v=SJ1Xuz9D-ZQ",
    "https://www.youtube.com/watch?v=SJ1Xuz9D-ZQ",
    "https://www.youtube.com/watch?v=9bR-elyolBQ&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=3",
    "https://www.youtube.com/watch?v=HE0ijmUc6Og&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=51",
    "https://www.youtube.com/watch?v=3WUUeM07i_Q&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=42",
    "https://www.youtube.com/watch?v=2W4ZNSwoW_4&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=2",
    "https://www.youtube.com/watch?v=jWxvty2KROs&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=7",
    "https://www.youtube.com/watch?v=DxBDqHL5NlM",
    "https://www.youtube.com/watch?v=dGKbTKLnym4&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=29",
    "https://www.youtube.com/watch?v=wQq3ybaLZeA&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=4",
    "https://www.youtube.com/watch?v=Fcbw82ykBvY&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=15",
    "https://www.youtube.com/watch?v=R08gYyypGto&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=13",
    "https://www.youtube.com/watch?v=DEyDbzSudEU&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=93",
    "https://www.youtube.com/watch?v=DxBDqHL5NlM",
    "https://www.youtube.com/watch?v=DJQGX2J4IVw&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=10",
    "https://www.youtube.com/watch?v=YslHgg2E-Ro&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=95",
    "https://www.youtube.com/watch?v=nYmUEJIBj3c&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=66",
    "https://www.youtube.com/watch?v=jgh6sGwtTwk",
    "https://www.youtube.com/watch?v=jgh6sGwtTwk",
    "https://www.youtube.com/watch?v=42bFodPahBU&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=76",
    "https://www.youtube.com/watch?v=JhX1nBnirNw&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=19",
    "https://www.youtube.com/watch?v=SaZK9vlSmHI",
    "https://www.youtube.com/watch?v=SaZK9vlSmHI",
    "https://www.youtube.com/watch?v=GQa_N7wft7M",
    "https://www.youtube.com/watch?v=EOf3cGIQpA4&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=71",
    "https://www.youtube.com/watch?v=pQUsUHvyoI0&list=PLoVy-85EFtK92qMfHTNZi0BAA3T1AbDys&index=43",
    ]


for item, link in zip(FullExerciseList, vid_links):
    button = Button(my_text1, text=item, command = lambda x=link: webbrowser.open_new(x)) 
    # inserting button into my_text1
    my_text1.window_create('end', window=button)
    # add a newline so that each button is in a separate line
    my_text1.insert('end', '\n')
    
root.mainloop()
Sriram Srinivasan
  • 650
  • 2
  • 4
  • 14