2

I'm making a main code in Python 3.6 called gui_check.py.

The code looks like this:

from tkinter import *
from urlread import givenumbers

top = Tk()
top.geometry("400x400")

B = Button(top, text = "Hello", command = givenumbers())
B.place(x = 50,y = 50)

top.mainloop()

In this code, there is a function called givenumbers() which is a function from another file (called urlread.py) that prints numbers.

The result I wanted to get is a gui with a button that when I click on it, it calls the function givenumber(). However, the result I get is that when I run the code it runs givenumber() (print the numbers) while opening the gui even without me clicking on the button.

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
Ben
  • 1,737
  • 2
  • 30
  • 61
  • Possible duplicate of [Why is Button parameter “command” executed when declared?](http://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared) – j_4321 Feb 03 '17 at 17:57

1 Answers1

2

Remove the parentheses in:

B = Button(top, text = "Hello", command = givenumbers())

So you should have:

B = Button(top, text = "Hello", command = givenumbers)

instead.

Read http://effbot.org/zone/tkinter-callbacks.htm

Leonid
  • 708
  • 5
  • 12