站点导航器,具有可点击的按钮和创建新按钮选项。

huangapple go评论97阅读模式
英文:

Site navigator that has clickable buttons and option to create new button

问题

UPDATE: 当使用 https://url.com 时它能正常工作,但是当使用 url.com 时不能正常工作。

我想要创建一个站点导航器,其中有可点击的按钮,这些按钮可以将我导航到相应按钮的网址。还有另一个按钮,当点击它时,它将弹出一个输入字段,我可以在其中输入网址和按钮的标题。原本放置的按钮是可以正常工作的,可以将我导航到网站。一切都正常工作,除非当我点击新创建的按钮时,它不能将我重定向到网站。

  1. import tkinter as tk
  2. import webbrowser
  3. def open_website(url):
  4. webbrowser.open(url)
  5. def add_button():
  6. # 隐藏“添加网站”按钮
  7. add_button.pack_forget()
  8. # 显示用于URL和标题的输入字段
  9. label_url.pack(anchor="center", padx=10, pady=5)
  10. entry_url.pack(anchor="center", padx=10, pady=5)
  11. label_title.pack(anchor="center", padx=10, pady=5)
  12. entry_title.pack(anchor="center", padx=10, pady=5)
  13. # 显示“创建按钮”按钮
  14. create_button.pack(anchor="center", padx=10, pady=5)
  15. def create_button():
  16. new_url = entry_url.get()
  17. new_title = entry_title.get()
  18. if new_url and new_title:
  19. button = tk.Button(root, text=new_title, command=lambda url=new_url: open_website(url), **button_style)
  20. button.pack(anchor="center", padx=10, pady=5)
  21. entry_url.delete(0, tk.END)
  22. entry_title.delete(0, tk.END)
  23. # 添加新按钮后隐藏输入字段和“创建按钮”按钮
  24. label_url.pack_forget()
  25. entry_url.pack_forget()
  26. label_title.pack_forget()
  27. entry_title.pack_forget()
  28. create_button.pack_forget()
  29. # 再次显示“添加新网站”按钮
  30. add_button.pack(anchor="center", padx=10, pady=5)
  31. root = tk.Tk()
  32. root.title("Navigator") # 设置窗口标题
  33. root.configure(bg="black") # 设置背景颜色为黑色
  34. def open_google():
  35. open_website("https://www.google.com")
  36. def open_facebook():
  37. open_website("https://www.facebook.com")
  38. def open_twitter():
  39. open_website("https://www.twitter.com")
  40. button_style = {
  41. "bg": "black", # 设置按钮背景颜色为黑色
  42. "fg": "white", # 设置按钮前景(文本)颜色为白色
  43. "activebackground": "darkred", # 设置按钮被点击或激活时的背景颜色
  44. "activeforeground": "white", # 设置按钮被点击或激活时的前景颜色
  45. "highlightbackground": "black", # 设置边框颜色为黑色
  46. "highlightcolor": "black", # 设置边框颜色为黑色
  47. "highlightthickness": 0, # 移除高亮边框
  48. "borderwidth": 0, # 移除默认按钮边框
  49. "font": ("Arial", 12, "bold") # 设置按钮字体和大小
  50. }
  51. button1 = tk.Button(root, text="Google", command=open_google, **button_style)
  52. button1.pack(anchor="center", padx=10, pady=5)
  53. button2 = tk.Button(root, text="Facebook", command=open_facebook, **button_style)
  54. button2.pack(anchor="center", padx=10, pady=5)
  55. button3 = tk.Button(root, text="Twitter", command=open_twitter, **button_style)
  56. button3.pack(anchor="center", padx=10, pady=5)
  57. # 新网站URL和标题的输入字段
  58. label_url = tk.Label(root, text="输入网址:", fg="white", bg="black")
  59. entry_url = tk.Entry(root, width=30)
  60. label_title = tk.Label(root, text="输入标题:", fg="white", bg="black")
  61. entry_title = tk.Entry(root, width=30)
  62. # 创建按钮按钮
  63. create_button = tk.Button(root, text="创建按钮", command=create_button, **button_style)
  64. # 添加网站按钮
  65. add_button = tk.Button(root, text="添加新网站", command=add_button, **button_style)
  66. add_button.pack(anchor="center", padx=10, pady=5)
  67. 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

  1. import tkinter as tk
  2. import webbrowser
  3. def open_website(url):
  4. webbrowser.open(url)
  5. def add_button():
  6. # Hide the "Add Website" button
  7. add_button.pack_forget()
  8. # Display the input fields for URL and title
  9. label_url.pack(anchor="center", padx=10, pady=5)
  10. entry_url.pack(anchor="center", padx=10, pady=5)
  11. label_title.pack(anchor="center", padx=10, pady=5)
  12. entry_title.pack(anchor="center", padx=10, pady=5)
  13. # Show the "Create Button" button
  14. create_button.pack(anchor="center", padx=10, pady=5)
  15. def create_button():
  16. new_url = entry_url.get()
  17. new_title = entry_title.get()
  18. if new_url and new_title:
  19. button = tk.Button(root, text=new_title, command=lambda url=new_url: open_website(url), **button_style)
  20. button.pack(anchor="center", padx=10, pady=5)
  21. entry_url.delete(0, tk.END)
  22. entry_title.delete(0, tk.END)
  23. # Hide the input fields and "Create Button" button after adding the new button
  24. label_url.pack_forget()
  25. entry_url.pack_forget()
  26. label_title.pack_forget()
  27. entry_title.pack_forget()
  28. create_button.pack_forget()
  29. # Show the "Add New Website" button again
  30. add_button.pack(anchor="center", padx=10, pady=5)
  31. root = tk.Tk()
  32. root.title("Navigator") # Set the title of the window
  33. root.configure(bg="black") # Set the background color to black
  34. def open_google():
  35. open_website("https://www.google.com")
  36. def open_facebook():
  37. open_website("https://www.facebook.com")
  38. def open_twitter():
  39. open_website("https://www.twitter.com")
  40. button_style = {
  41. "bg": "black", # Set button background color to black
  42. "fg": "white", # Set button foreground (text) color to white
  43. "activebackground": "darkred", # Set background color when button is clicked or activated
  44. "activeforeground": "white", # Set foreground color when button is clicked or activated
  45. "highlightbackground": "black", # Set border color to black
  46. "highlightcolor": "black", # Set border color to black
  47. "highlightthickness": 0, # Remove the highlight border
  48. "borderwidth": 0, # Remove the default button border
  49. "font": ("Arial", 12, "bold") # Set the button font and size
  50. }
  51. button1 = tk.Button(root, text="Google", command=open_google, **button_style)
  52. button1.pack(anchor="center", padx=10, pady=5)
  53. button2 = tk.Button(root, text="Facebook", command=open_facebook, **button_style)
  54. button2.pack(anchor="center", padx=10, pady=5)
  55. button3 = tk.Button(root, text="Twitter", command=open_twitter, **button_style)
  56. button3.pack(anchor="center", padx=10, pady=5)
  57. # Entry fields for new website URL and title
  58. label_url = tk.Label(root, text="Enter URL:", fg="white", bg="black")
  59. entry_url = tk.Entry(root, width=30)
  60. label_title = tk.Label(root, text="Enter Title:", fg="white", bg="black")
  61. entry_title = tk.Entry(root, width=30)
  62. # Create Button button
  63. create_button = tk.Button(root, text="Create Button", command=create_button, **button_style)
  64. # Add Website button
  65. add_button = tk.Button(root, text="Add New Website", command=add_button, **button_style)
  66. add_button.pack(anchor="center", padx=10, pady=5)
  67. root.mainloop()

答案1

得分: -1

你缺少了 open_website

  1. def create_button():
  2. new_url = entry_url.get()
  3. new_title = entry_title.get()
  4. 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.

  1. def create_button():
  2. new_url = entry_url.get()
  3. new_title = entry_title.get()
  4. open_website(new_url) # Add this.
  5. :
  6. :
  7. :

huangapple
  • 本文由 发表于 2023年6月8日 05:14:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定