-1

I wanted to create a buttons using tkinter with line of code below

from tkinter import *
import webbrowser
import numpy as np
course = []
course_link = []
fh = open(r'D:\E-Learning\IBM Data Science.txt')
i=1
for line in fh:
   f=line.rstrip()
   if not line.startswith("https:"):
    
    course.append( "Course " + str(i) + ": " + str(f))
    i+=1
  else:
    course_link.append(str(f))
window = Tk()
for i in range(0,2):
  course[i] = Button(window,text=str(course[i]))
  course[i].config(command=webbrowser.open(str(course_link[i])))
  course[i].pack()

window.mainloop()

Thanks guys Im new to this module so it's amazing if you can help me out! Edit: Pressing the buttons won't do the command too.

1 Answers1

1

Its because you are calling it instead of binding it

for i in range(0,2):
    def onClick(i=i):# not good practice but demonstrates
        print("You clicked I",i)
    course[i] = Button(window,text=str(course[i]))
    course[i].config(command=onClick) # notice i dont call onClick
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179