Hey guys, I've started using Tkinter now and I've came across a problem...Here in the code below i just want a drop box style menu to appear and when i click that option, theres more options. When i click for example their would be a box called Addition, you would click it and it would say Goto Addition, and when you click it, it will execute that choice in the shell. Heres what I've started on:

from Tkinter import *

root = Tk()

#The options box
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add(label="Addition", command='1')
filemenu.add(label="Subtraction, command='2')
             filemenu.add_seperator()
             
root.config(menu=menubar)

mainloop()
             
#The Start of the calculator
a = 0

def menu():
    print "1) Addition"
    print "2) Subtraction"
    print ""
    print "3) Multiplication"
    print "4) Division"
    print ""
    print "5) Exponets"
    
while a == 0:
    menu()
    a = int(raw_input("Choose your option: "))

    if a == 1:
        x = 0
        number = int(raw_input("Add this: "))
        while x == 0:
            number1 = int(raw_input("By this: "))
            number2 = number+number1
            print number2
            x+=1
            if x == 1:
                a = 0

    elif a == 2:
        x = 0
        number3 = int(raw_input("Subtract this: "))
        while x == 0:
            number4 = int(raw_input("By this: "))
            number5 = number3-number4
            print number5
            x+=1
            if x == 1:
                a = 0

    elif a == 3:
        x = 0
        number6 = int(raw_input("Multiply this: "))
        while x == 0:
            number7 = int(raw_input("By this: "))
            number8 = number6*number7
            print number8
        x+=1
        if x == 1:
                a = 0

    elif a == 4:
        x = 0
        number9 = int(raw_input("Divide this: "))
        while x == 0:
            number10 = int(raw_input("By this: "))
            number11 = number9/number10
            print number11
            x+=1
            if x == 1:
                a = 0

    elif a == 5:
        x = 0
        number12 = int(raw_input("Exponet1: "))
        while x == 0:
            number13 = int(raw_input("Exponet2: "))
            number14 = number12**number13
            print number14
            x+=1
            if x == 1:
                a = 0

Recommended Answers

All 3 Replies

Hello, I have looked through your code and made a few modifications. I have added the operation buttons to the tk window, and, messing the code up myself, have found a way to make it work.

from Tkinter import *

root = Tk()

#The Start of the calculator using globals
a = 0
global a
glob=globals()

#We need to make the commands Tkinter uses, also, we need to use global to make a global.
def cmd1():
    glob['a'] = 1
    root.quit()
def cmd2():
    glob['a'] = 2
    root.quit()
def cmd3():
    glob['a'] = 3
    root.quit()
def cmd4():
    glob['a'] = 4
    root.quit()
def cmd5():
    glob['a'] = 5
    root.quit()

#The options box
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Addition", command=cmd1)
filemenu.add_command(label="Subtraction", command=cmd2)
filemenu.add_separator()
filemenu.add_command(label="Multiplication", command=cmd3)
filemenu.add_command(label="Division", command=cmd4)
filemenu.add_separator()
filemenu.add_command(label="Exponets", command=cmd5)
filemenu.add_command(label="Quit", command=root.quit)

#Make the dropdown menu button
menubar.add_cascade(label="Operation", menu=filemenu)

root.config(menu=menubar)

root.mainloop()

#update globals!
glob=globals()

#Now start the loop.    
while glob['a'] != 0:
    #update globals again...
    glob=globals()
    if glob['a'] == 1:
        x = 0
        number = int(raw_input("Add this: "))
        while x == 0:
            number1 = int(raw_input("By this: "))
            number2 = number+number1
            print number2
            x+=1
            if x == 1:
                glob['a'] = 0
        root.mainloop()

    elif glob['a'] == 2:
        x = 0
        number3 = int(raw_input("Subtract this: "))
        while x == 0:
            number4 = int(raw_input("By this: "))
            number5 = number3-number4
            print number5
            x+=1
            if x == 1:
                glob['a'] = 0
        root.mainloop()

    elif glob['a'] == 3:
        x = 0
        number6 = int(raw_input("Multiply this: "))
        while x == 0:
            number7 = int(raw_input("By this: "))
            number8 = number6*number7
            print number8
            x+=1
            if x == 1:
                glob['a'] = 0
        root.mainloop()

    elif glob['a'] == 4:
        x = 0
        number9 = int(raw_input("Divide this: "))
        while x == 0:
            number10 = int(raw_input("By this: "))
            number11 = number9/number10
            print number11
            x+=1
            if x == 1:
                glob['a'] = 0
        root.mainloop()

    elif glob['a'] == 5:
        x = 0
        number12 = int(raw_input("Exponet1: "))
        while x == 0:
            number13 = int(raw_input("Exponet2: "))
            number14 = number12**number13
            print number14
            x+=1
            if x == 1:
                glob['a'] = 0
        root.mainloop()

If you want me to explain why root.mainloop is there so many time, or anything else, just ask.

Thanks Cap.Micro you've just gave me tons of ideas from that code :) and i've never seen the use of globals, do you think you can explain briefly?

Hello again.
Well, heres a brief explanation of globals, using def addOne(): as an example.

Say I want to add 1 to a variable OUTSIDE the def addOne() statement like this:

var = 0
def addOne():
    var += 1
    print var

This will not give an error, or add to the variable because python thinks var is an input to the def addOne() statement. To fix this, we use globals. Globals are basically a variable that is available to every thing in the code. To make a variable global, just do global <var name>.
The <var name> needs to be made already, so completely like this:

var = 0
global var

Now we need to implement thins in out code, to do this, we need to get a variable from a global by using globals(). Here is how: make a variable of globals by doing glob=globals().
Now, to get the variable we us a dictionary like function: glob . So, lets implement it!

#this makes a global variable name var with a value of 0
var = 0
global var

#Now our function.
def addOne():
    #Use globals by making a global variable.
    glob=globals()
    #Add one to it.
    glob['var'] += 1
    #Print the global now.
    print glob['var']
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.