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()
有没有办法制作另一个带有其他链接的按钮? 我知道会有,但我似乎无法弄清楚
答案 0 :(得分:0)
通过阅读评论,您似乎不确定如何在Tkinter窗口中创建多个按钮。
它非常简单,您需要做的就是重复用于创建第一个按钮的过程。
我将提供2个例子。
以下是为每个要为其提供按钮的网站手动创建每个按钮的方法。
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()
以下是从列表创建按钮的示例。这是一个简单的例子,但它应该足以说明如何完成它。
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()