top of page
Click here to go to the home page of AskTheCode.

How to create button in python using tkinter | AskTheCode

Team ATC

How to create button in python using tkinter | Python GUI Program | AskTheCode

 

Problem:

Write a Python program that creates a GUI with a single button. When the button is pressed it should create a second button. When that button is pressed, it should create a label that says, “Nice Job!”.


Note: This program needs you to understand tkinter module, how to create button in python using tkinter, and how to call a function on button click in python.

 

EXPLANATION:

According to the problem, we have to create a Python program, in which, for the initial frame/window, there should be a button, and when we click on it, then a new button should be created. And, when this new button is pressed, then a dialogue should be displayed on the screen, i.e. "Nice Job!”.


In our python program, we create an interface "root". We create a button a button, let's say "START", and to deal what will happen when it is clicked. We already know how to call a function on button click in python, so will implement the logic in creating an another button. And, similarly, we'll handle when to display the message.

from tkinter import *

To work with tkinter, we'll import the whole tkinter module. And, to locate buttons and label for our Python GUI program, we'll create the interface/window:

root = Tk()

Afterwards, we'll create the first button using:

button = Button(root)
button.pack()

As we've to perform an action on click of this button, so, will create it like this:

button = Button(root, text="START", command=buttonClicked)
button.pack(side = TOP)

Here, "buttonClicked" is the name of our function, that we'll call when this button is clicked.

The buttonClicked function can be defined as:

def buttonClicked():
    global root
    global count
    button = Button(root, text="SHOW", command=newButtonClicked)
    button.pack(side = TOP)

Now, after execution of this code, a new button "SHOW" will be created.

When this new button is clicked, then a dialog is to be displayed. So, we are again calling an action "newButtonClicked", and this newButtonClicked function will be defined as:

def newButtonClicked():
    global root
    label = Label(root, text="Nice Job!")
    label.pack()

Now, we are done with the elementary portion for this problem. Below is the sophisticated program for this problem, the code given below can be innovatory enhanced further.

 

Code:

from tkinter import *

root = None
count = 0

def newButtonClicked():
    global root, count
    if count == 1:
        label = Label(root, text="Nice Job!")
        label.pack()
    count += 1

def buttonClicked():
    global root
    global count
    if count < 1:
        button = Button(root, activeforeground = "blue",activebackground = "pink", text="SHOW", command=newButtonClicked)
        button.pack(side = TOP)
    count += 1

def main():
    global root
    root = Tk()
    root.title("ATC Basic tkinter")
    button = Button(root, activeforeground = "blue",activebackground = "pink", text="START", command=buttonClicked)
    button.pack(side = TOP)
    root.mainloop()

main()
#This program allows to create button and display dialogue only once

Recent Posts

See All

Comments


bottom of page