Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • CreatiCode
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

CreatiCode Scratch Forum

  1. CreatiCode Forum
  2. Knowledge Base
  3. 2D Blocks
  4. The Button Widget

The Button Widget

Scheduled Pinned Locked Moved 2D Blocks
1 Posts 1 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CreatiCodeI Offline
    CreatiCodeI Offline
    CreatiCode
    wrote on last edited by admin
    #1

     

    Prerequisite

    • The Label Widget
    • Setting Text on Widget

     

    Description

    You can add buttons to the stage, and specify what happens when the user clicks on them.

     

    Adding a Button

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/e8a4cb58-2dd7-4da6-b539-23816b7f69c0.png" width="800" style="">

     

    Parameters

    • Text on Button: The text to be displayed on the button.
    • Center Position: The x and y positions of the center point of the button.
    • Size: The width and height of the button. Note that the button is limited to the stage window, which has a width of 480 and a height of 360.
    • Tooltip: The text to be displayed when the mouse is hovering over the button. This is often useful when you want to show some information about what the button does before the user clicks on it.
    • Button Name: A unique name to refer to this button later.

     
     

    Handle the Button Click Event

    This block allows you to run some code whenever the button is clicked.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/352d93cb-7617-4c62-90a7-205f43006bf1.png" width="450" style="">

     

    Parameters

    • Widget Name: You need to select the name of the button for which you are waiting for the click event. That button has to be added to the stage already for its name to show up in this list.

     

    Demo

    This screencast shows how to add a button, and then change the text on it whenever the user clicks on it.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/0e064b2e-0608-4824-94aa-cbea28889119.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">

    To make the text toggle between “Start” and “Stop”, a variable “Command” is used to keep track of the current command. Each time the button is clicked, we change “Command” based on its current value.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/ea613e9d-a50f-4ab2-bbb1-f56fa1c9b105.png" width="800" style="">

     

    Try it yourself:

    https://play.creaticode.com/projects/11550cc1f7d8230ae73123af

     
     

    When Any Button Clicked

    Sometimes there are many buttons on the stage, and we don’t want to write a click-handler for each of them. Instead, we can use the following hat block to capture all button click events:

    0a67e97f-3f9a-48bd-ac10-39ce31d0f47e-image.png

     
    To use this block, a variable has to be defined first, such as “my variable”.

    With this block, when any button on the stage is clicked, this block will be triggered, and the name of the button will be stored in the variable we have specified.

     

    Example

    For example, in this program, we have 2 buttons named “button1” and “button2”. When I click on either one of them, the “when any button named ( ) clicked” block will be triggered, and the exact name of that button is stored in the “button name” variable.

    ed050a73-7303-4f15-a907-28de4a38d8a4-image.png

     
    Here is what we get when we run this program:

    b1.gif

    1 Reply Last reply
    0
    • CreatiCodeI CreatiCode pinned this topic on

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode