0

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Josh K
  • 11
  • 2
  • 2
    Your problem is quite clear. You're trying to use `contineFunc` before you defined it. Move your definition of `contineFunc` _ before_ you try to use it. – Christian Dean Jan 22 '18 at 01:40
  • I think you need to start simpler than that, above code needs extra line(s) to run after the method(s) been moved up. – Nae Jan 22 '18 at 02:14
  • The windows shouldn't pop up, are you on IDLE? – Nae Jan 22 '18 at 02:42
  • First 3 lines of your code can be reduced to 1, ideally keep `import tkinter` imo. – Nae Jan 22 '18 at 02:58
  • Ideally, [you shouldn't have multiple instances of `Tk`](https://stackoverflow.com/q/48045401/7032856). You can use `tkinter.Toplevel` for additional windows you may want to use. – Nae Jan 22 '18 at 03:01
  • `welcomeLabel.pack()` and the others shouldn't need to be in `print` to function. – Nae Jan 22 '18 at 03:05
  • `mainloop` has never been called, your GUI is not supposed to show up. Ideally `mainloop` needs to be the last thing the code is running. I'd add `mainBox.mainloop()` as the last line. – Nae Jan 22 '18 at 03:07
  • Try to create a [mcve] for the error(s) you have. The process points one in the direction the error is caused. – Nae Jan 22 '18 at 03:10
  • `if welcomeEntry != int` tries to compare an `Entry` object to a class type `int`. In order to get the string in an entry, you can use `.get()` method on its object, as in: `welcomeEntry.get()` returns the string that is written in the `Entry` object, which you can then compare to a string like `if welcomeEntry.get() == "some string":`. – Nae Jan 22 '18 at 03:13

1 Answers1

1

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()
Nae
  • 14,209
  • 7
  • 52
  • 79