0

I wanted to make button in tkinter, but when I started program, the command always calls when code just starts. Here is example code:

import tkinter as tk
from tkinter import messagebox

window = tk.Tk()
window.title("pls work")
window.wm_geometry("100x100")

def message():
    messagebox.showinfo("Hi there")

button = tk.Button(text="Hello", command=message())
button.grid(column=0, row=0)

while True:
    window.update()

After, button doesn't work anymore.

I don't know what I'm doing wrong, so I need help.

ColorfulYoshi
  • 89
  • 1
  • 2
  • 9

4 Answers4

1

The command should be a pointer to a function

In the code you wrote, the command gets the return value from the function.

command=message()

The correct way is

command = message
0

The problem is you are requesting a return value from the fucnction. Try using this.

from tkinter import *

# import messagebox from tkinter module
import tkinter.messagebox

# create a tkinter root window
root = tkinter.Tk()

# root window title and dimension
root.title("When you press a button the message will pop up")
root.geometry('75x50')

# Create a messagebox showinfo

def onClick(): 
   tkinter.messagebox.showinfo("Hello World!.",  "Hi I'm your message")

# Create a Button
button = Button(root, text="Click Me", command=onClick, height=5, width=10)

 # Set the position of button on the top of window.
 button.pack(side='top')
 root.mainloop()
CodeWithYash
  • 223
  • 1
  • 15
0

You have 2 errors:

first: It must be command=message

second: You must give a message argument too, you entered a title only.

Ali FGT
  • 55
  • 8
0

Or, what you can do is. Add another variable.

command = message()

Before this line,

button = tk.Button(text="Hello", command=message())

And chande this line to,

button = tk.Button(text="Hello", command=command)
CodeWithYash
  • 223
  • 1
  • 15