如何从输入框中获取自定义输入以形成特定的超链接?

2024-04-19 12:51:14 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我第一次编写任何东西,因为我必须很快完成这个项目,所以我没有一个合适的Python教程

事情是这样的:我需要编程一个带有两个输入框和一个按钮的小窗口

该按钮需要将条目添加到预先确定的超链接中。例如:

条目1:2020 条目2:12345

当我点击按钮时,它会打开,比如说,http://www.google.com/2020-12345.html

到目前为止,我的情况如下:

# !/usr/bin/python3  

from tkinter import *

top = Tk()

import webbrowser

new = 1
url = "http://www.google.com/e1-e2"

def openweb():
    webbrowser.open(url,new=new)

top.geometry("250x100")

name = Label(top, text="Numbers").place(x=50, y=1)

sbmitbtn = Button(top, text="Submit", command=openweb).place(x=90, y=65)

e1 = Entry(top).place(x=20, y=20)
e2 = Entry(top).place(x=20, y=45)

top.mainloop()

1条回答
网友
1楼 · 发布于 2024-04-19 12:51:14

像这样:

from tkinter import *
import webbrowser

# Use {} as a placeholder to allow format() to fill it in later
url_template = "http://www.google.com/{}-{}"

def openweb():
    url = url_template.format(e1.get(), e2.get())
    webbrowser.open(url,new=1)

top = Tk()
top.geometry("250x100")

# Do not use place(). It's very buggy and hard to maintain. Use pack() or grid()
Label(top, text="Numbers").pack()

# You must use 2 lines for named Widgets. You cannot layout on the same line
e1 = Entry(top)
e1.pack()
e2 = Entry(top)
e2.pack()

sbmitbtn = Button(top, text="Submit", command=openweb)
sbmitbtn.pack()

top.mainloop()

相关问题 更多 >