Home > Software design >  How can i create a button that creates another button with a link?
How can i create a button that creates another button with a link?

Time:09-28

i'm trying to make a desktop assistant in python that has some buttons that shortcut me some things. But i can't figure out how to make a button that creates another button with the url i need.

There is the code i tried to use but it generates an error:

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()

I didn't finished yet the function to get the url cause i'm still searching a way to add a variable in the command=openweb part, because i declared all the sites that i need with a variable as strings. If somebody knows that also it would a great help

Thanks

Update:

the error generated is this one:

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()

CodePudding user response:

I think the issue may be caused by having both a method and a variable named button_text; the parent class (not shown, but I assume it exists due to the use of self) doesn't know if self.button_text refers to the method or the variable

  • Related