我正在嘗試用python制作一個桌面助手,它有一些按鈕可以讓我快捷地做一些事情。但我不知道如何制作一個按鈕,用我需要的 url 創建另一個按鈕。
有我嘗試使用的代碼,但它會產生錯誤:
def openweb():
webbrowser.open_new_tab(url)
def button_text(self):
text= self.entry_text.get()
self.button_text.set(text)
def new_button():
self.entry_text= StringVar()
entry= Entry(app, textvariable= self.entry_text)
self.button_text= StringVar()
n_button = Button(root, textvariable= self.button_text, command=openweb)
#Buttons
googlebutton= Button(app, text="Google")
googlebutton.pack()
youtubebutton= Button(app, text="Youtube")
youtubebutton.pack()
newbutton= Button(app, text="Add a Button",
command=new_button)
newbutton.pack()
app.mainloop()
我還沒有完成獲取 url 的函式,因為我仍在尋找一種在 command=openweb 部分中添加變數的方法,因為我將我需要的所有站點都宣告為字串。如果有人知道這也會有很大幫助
謝謝
更新:
產生的錯誤是這個:
Traceback (most recent call last):
File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\Users\danie\source\repos\Desktop_app\Desktop_app\Desktop_app.py", line 57, in new_button
new_button_text = button_text()
UnboundLocalError: local variable 'button_text' referenced before assignment
local variable 'button_text' referenced before assignment
Analisi dello stack:
> File "C:\Users\danie\source\repos\Desktop_app\Desktop_app\Desktop_app.py", line 57, in new_button (Current frame)
> new_button_text = button_text()
> File "C:\Users\danie\source\repos\Desktop_app\Desktop_app\Desktop_app.py", line 89, in
<module>
> app.mainloop()
uj5u.com熱心網友回復:
我認為這個問題可能是由一個方法和一個名為的變數引起的button_text;父類(未顯示,但我假設它由于使用而存在self)不知道self.button_text是指方法還是變數
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/510579.html
