如何为列表中的每个按钮创建超链接? [Tkinter]

How do i create hyperlinks for every button in a list? [Tkinter]

我昨天制作了一个按钮列表,我想通过为列表中的每个项目添加一个超链接来稍微加强它。然而,按下每个按钮都没有任何结果(事实上,我相信 'None' 是由此产生的)。所有的 YouTube 链接都被制作成一个数组,并且对于每个项目,我都尝试分配一个超链接。

这里是代码:

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')

非常感谢任何帮助 =)

我对您的代码进行了以下修改:

  1. 已删除 callback 并使用 lambda 打开相应的 YouTube 视频。
  2. 使用zip来pair-wise提取练习名称和视频link。

如果您想知道我为什么写 lambda x=link: webbrowser.open_new(x) 而不是 lambda : webbrowser.open_new(link),请参阅此


工作代码:

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()