英文:
Site navigator that has clickable buttons and option to create new button
问题
UPDATE: 当使用 https://url.com
时它能正常工作,但是当使用 url.com
时不能正常工作。
我想要创建一个站点导航器,其中有可点击的按钮,这些按钮可以将我导航到相应按钮的网址。还有另一个按钮,当点击它时,它将弹出一个输入字段,我可以在其中输入网址和按钮的标题。原本放置的按钮是可以正常工作的,可以将我导航到网站。一切都正常工作,除非当我点击新创建的按钮时,它不能将我重定向到网站。
import tkinter as tk
import webbrowser
def open_website(url):
webbrowser.open(url)
def add_button():
# 隐藏“添加网站”按钮
add_button.pack_forget()
# 显示用于URL和标题的输入字段
label_url.pack(anchor="center", padx=10, pady=5)
entry_url.pack(anchor="center", padx=10, pady=5)
label_title.pack(anchor="center", padx=10, pady=5)
entry_title.pack(anchor="center", padx=10, pady=5)
# 显示“创建按钮”按钮
create_button.pack(anchor="center", padx=10, pady=5)
def create_button():
new_url = entry_url.get()
new_title = entry_title.get()
if new_url and new_title:
button = tk.Button(root, text=new_title, command=lambda url=new_url: open_website(url), **button_style)
button.pack(anchor="center", padx=10, pady=5)
entry_url.delete(0, tk.END)
entry_title.delete(0, tk.END)
# 添加新按钮后隐藏输入字段和“创建按钮”按钮
label_url.pack_forget()
entry_url.pack_forget()
label_title.pack_forget()
entry_title.pack_forget()
create_button.pack_forget()
# 再次显示“添加新网站”按钮
add_button.pack(anchor="center", padx=10, pady=5)
root = tk.Tk()
root.title("Navigator") # 设置窗口标题
root.configure(bg="black") # 设置背景颜色为黑色
def open_google():
open_website("https://www.google.com")
def open_facebook():
open_website("https://www.facebook.com")
def open_twitter():
open_website("https://www.twitter.com")
button_style = {
"bg": "black", # 设置按钮背景颜色为黑色
"fg": "white", # 设置按钮前景(文本)颜色为白色
"activebackground": "darkred", # 设置按钮被点击或激活时的背景颜色
"activeforeground": "white", # 设置按钮被点击或激活时的前景颜色
"highlightbackground": "black", # 设置边框颜色为黑色
"highlightcolor": "black", # 设置边框颜色为黑色
"highlightthickness": 0, # 移除高亮边框
"borderwidth": 0, # 移除默认按钮边框
"font": ("Arial", 12, "bold") # 设置按钮字体和大小
}
button1 = tk.Button(root, text="Google", command=open_google, **button_style)
button1.pack(anchor="center", padx=10, pady=5)
button2 = tk.Button(root, text="Facebook", command=open_facebook, **button_style)
button2.pack(anchor="center", padx=10, pady=5)
button3 = tk.Button(root, text="Twitter", command=open_twitter, **button_style)
button3.pack(anchor="center", padx=10, pady=5)
# 新网站URL和标题的输入字段
label_url = tk.Label(root, text="输入网址:", fg="white", bg="black")
entry_url = tk.Entry(root, width=30)
label_title = tk.Label(root, text="输入标题:", fg="white", bg="black")
entry_title = tk.Entry(root, width=30)
# 创建按钮按钮
create_button = tk.Button(root, text="创建按钮", command=create_button, **button_style)
# 添加网站按钮
add_button = tk.Button(root, text="添加新网站", command=add_button, **button_style)
add_button.pack(anchor="center", padx=10, pady=5)
root.mainloop()
英文:
UPDATE: It is working when https://url.com
but it doesnt when url.com
I want to make a site navigator that has clickable buttons which navigate me to the corresponding url of button. And also has another button that when clicked it will pop an input field that i can type the url and the title of the button. A buttons that originaly placed are working and navigate me to website. Everythings works except when i click the new created button it doesnt redrict me to website
import tkinter as tk
import webbrowser
def open_website(url):
webbrowser.open(url)
def add_button():
# Hide the "Add Website" button
add_button.pack_forget()
# Display the input fields for URL and title
label_url.pack(anchor="center", padx=10, pady=5)
entry_url.pack(anchor="center", padx=10, pady=5)
label_title.pack(anchor="center", padx=10, pady=5)
entry_title.pack(anchor="center", padx=10, pady=5)
# Show the "Create Button" button
create_button.pack(anchor="center", padx=10, pady=5)
def create_button():
new_url = entry_url.get()
new_title = entry_title.get()
if new_url and new_title:
button = tk.Button(root, text=new_title, command=lambda url=new_url: open_website(url), **button_style)
button.pack(anchor="center", padx=10, pady=5)
entry_url.delete(0, tk.END)
entry_title.delete(0, tk.END)
# Hide the input fields and "Create Button" button after adding the new button
label_url.pack_forget()
entry_url.pack_forget()
label_title.pack_forget()
entry_title.pack_forget()
create_button.pack_forget()
# Show the "Add New Website" button again
add_button.pack(anchor="center", padx=10, pady=5)
root = tk.Tk()
root.title("Navigator") # Set the title of the window
root.configure(bg="black") # Set the background color to black
def open_google():
open_website("https://www.google.com")
def open_facebook():
open_website("https://www.facebook.com")
def open_twitter():
open_website("https://www.twitter.com")
button_style = {
"bg": "black", # Set button background color to black
"fg": "white", # Set button foreground (text) color to white
"activebackground": "darkred", # Set background color when button is clicked or activated
"activeforeground": "white", # Set foreground color when button is clicked or activated
"highlightbackground": "black", # Set border color to black
"highlightcolor": "black", # Set border color to black
"highlightthickness": 0, # Remove the highlight border
"borderwidth": 0, # Remove the default button border
"font": ("Arial", 12, "bold") # Set the button font and size
}
button1 = tk.Button(root, text="Google", command=open_google, **button_style)
button1.pack(anchor="center", padx=10, pady=5)
button2 = tk.Button(root, text="Facebook", command=open_facebook, **button_style)
button2.pack(anchor="center", padx=10, pady=5)
button3 = tk.Button(root, text="Twitter", command=open_twitter, **button_style)
button3.pack(anchor="center", padx=10, pady=5)
# Entry fields for new website URL and title
label_url = tk.Label(root, text="Enter URL:", fg="white", bg="black")
entry_url = tk.Entry(root, width=30)
label_title = tk.Label(root, text="Enter Title:", fg="white", bg="black")
entry_title = tk.Entry(root, width=30)
# Create Button button
create_button = tk.Button(root, text="Create Button", command=create_button, **button_style)
# Add Website button
add_button = tk.Button(root, text="Add New Website", command=add_button, **button_style)
add_button.pack(anchor="center", padx=10, pady=5)
root.mainloop()
答案1
得分: -1
你缺少了 open_website
。
def create_button():
new_url = entry_url.get()
new_title = entry_title.get()
open_website(new_url) # 添加这行。
英文:
> Everythings works except when i click the new created button it doesnt
> redrict me to website
You are missing open_website
.
def create_button():
new_url = entry_url.get()
new_title = entry_title.get()
open_website(new_url) # Add this.
:
:
:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论