Josh K
Josh K

Reputation: 11

Tying a button to a function in Python (Tkinter)

I am currently just beginning to learn python and I thought I may try to build a basic calculator so I can learn a little bit of GUI concepts but also back-end concepts as well. Here is the code I have so far:

import tkinter
from tkinter import *
from tkinter import Button
import sys
import math

mainBox = Tk()
ment = StringVar()


mainBox.geometry("400x200")
mainBox.title("HandyCalc")

#Welcome screen buttons
welcomeLabel = Label(mainBox, text="Welcome to HandyCalc! Input your first 
number here: ")
welcomeEntry = Entry()
welcomeContinue = Button(mainBox, text = "Continue", command = continueFunc)

def continueFunc():
if welcomeEntry != int:
    intErr = Tk()


def welcome ():
print(welcomeLabel.pack())
print(welcomeEntry.pack())
print(welcomeContinue.pack())




def main():
welcome()

main()

When I run this program, the window pops up but the console returns an error of "contineFunc is not defined". I have researched numerous articles with similar problems and have had no such luck so far. Appreciate any help.

Upvotes: 0

Views: 1424

Answers (1)

Nae
Nae

Reputation: 15325

Below code ties a button to a function(that prints a string for demo), and it does just that:

import tkinter as tk


def function():
    print("This is invoked by a button tied to a function.")

root = tk.Tk()

button = tk.Button(root, text="My Button", command=function)
button.pack()

root.mainloop()

Upvotes: 1

Related Questions