-1
from tkinter import *
from tkinter import ttk
import webbrowser
import urllib

root = Tk()
 w = Label(root, text="Where can I take you?")
 w.pack()

url = 'http://sampdm.net/member.php?action=profile&uid=1'
def Openurl(url):
webbrowser.open_new(url)

button = Button(root, text = "Open Owners Profile #1", command = lambda: 
Openurl(url))

button.pack()
root.mainloop()

Is there any way to make another button with another link like it? I know there will be but I can't seem to figure out

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
QuantumCore
  • 21
  • 1
  • 5

1 Answers1

0

From reading the comments it looks like you are not sure how to create multiple buttons inside your Tkinter window.

Its really easy and all you need to do is repeat the process that you used to create your first button.

I will provide 2 examples.

Here is how you can create each button manually for each website you wish to provide a button for.

from tkinter import *
import webbrowser


root = Tk()

url = 'http://sampdm.net/member.php?action=profile&uid=1'
url2 = 'www.google.com' # assign any variable name you want.
some_other_url = 'https://www.yahoo.com/'

def Openurl(url):
    webbrowser.open_new(url)

w = Label(root, text="Where can I take you?")
w.pack()

# Here you can keep creating buttons using the same method as the first button.
# Just make sure you assign the correct url variable to the command.
button = Button(root, text = "Open Owners Profile #1", command = lambda: Openurl(url)).pack()
button2 = Button(root, text = "Open Google", command = lambda: Openurl(url2)).pack()
some_other_button = Button(root, text = "Open something else", command = lambda: Openurl(some_other_url)).pack()

root.mainloop()

Here is an example where your buttons are created from a list. This is a simple example but it should be enough to illustrate how it can be done.

from tkinter import *
import webbrowser


root = Tk()

# Here we create a list to use in button creation.
url_list = ['www.google.com', 'www.yahoo.com']

w = Label(root, text="Where can I take you?")
w.pack()

def Openurl(url):
    webbrowser.open_new(url)

def create_buttons():
    for url in url_list: # for each string in our url list
        # creating buttons from our for loop values that will pack
        # in the order they are created.
        Button(root, text = url, command = lambda u = url: Openurl(u)).pack()

create_buttons()

root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79