-1

I have a problem, I want to make a fontions which opens a site when I press the button except that as soon as I launch the file the functions are executed by themselves

from tkinter import *
import random
import string
import webbrowser

def Tokens():
    webbrowser.open_new("https://xlean.me")
    
button = "Start"

windows = Tk()
windows.title("Discord Tokens")
windows.geometry("500x150")
windows.minsize(500,150)
windows.maxsize(500,150)
windows.iconbitmap("icon.ico")
windows.config(background="#242424")

text = Label(windows, text="Hello to you !", bg="#242424", fg='white')
text.config(font=("Alatsi", 30))
text.pack()

button = Button(windows, text=button, bg='#202020', fg='white', command=Tokens())
button.pack(fill=X)

windows.mainloop()
Esteb
  • 5
  • 2
  • 1
    Does this answer your question? [Why is Button parameter “command” executed when declared?](https://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared) – Demian Wolf Aug 19 '20 at 23:16

1 Answers1

1

You should pass the Tokens function as the command arg, while you are passing the result of its execution instead. You need to remove the parenthesis after command=Tokens. Here is the fixed code:

from tkinter import *
import random
import string
import webbrowser

def Tokens():
    webbrowser.open_new("https://xlean.me")
    
button = "Start"

windows = Tk()
windows.title("Discord Tokens")
windows.geometry("500x150")
windows.minsize(500,150)
windows.maxsize(500,150)
windows.iconbitmap("icon.ico")
windows.config(background="#242424")

text = Label(windows, text="Hello to you !", bg="#242424", fg='white')
text.config(font=("Alatsi", 30))
text.pack()

button = Button(windows, text=button, bg='#202020', fg='white', command=Tokens)
button.pack(fill=X)

windows.mainloop()
Demian Wolf
  • 1,698
  • 2
  • 14
  • 34