Making a GUI Calculator in Python with Tkinter

Posted on 2009-02-21
Last Modified: 2012-05-06
i'm just starting out with python today, and am trying to make a basic calculator program in Python with the Tkinter gui module.

At the moment I have the buttons 1-9  and each one calls a separate function, i.e. OnButtonClick1, OnButtonClick2, etc.

However since they all basically do the same thing, and only the number is different, there must me an easier way to do this?
Is there any way that I can just have one function such as OnButtonClick which can determine which button was pressed and do its logic based on that?

Question by:liamrichards
    1 Comment
    LVL 39

    Accepted Solution

    You can have the same method handling all button clicks.

    Take a look at this example for inspiration:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
    When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now