Using functions within a GUI - how to pass and return variables to widgets
<p>SOLVED - answer found @ <a href="https://stackoverflow.com/questions/13114359/how-to-take-input-from-tkinter">How to take input from Tkinter</a></p>

<p>ONE GOTCHA - I tried copying the example's style in the solved link, though I kept some of my own formating.  I found that if the object that is getting the returned value has been created with any other dot added to it, for example putting <strong>.pack()</strong> or in my case <strong>.grid()</strong> on the same line as the object is created, it will throw out an error when you try to pass the variable back.  So, go ahead and use a second line to <strong>.pack()</strong> or <strong>.grid()</strong>.</p>

<p><strong>ORIGINAL POST</strong> </p>

<p>I've got a program that works fine on the command line and I am attempting to give it a GUI.  Unfortunately, I'm still pretty confused about how to use functions / methods / commands inside the interface.</p>

<p>My example is to use a button to get everything rolling after getting input from the user.  In the command line program, it was a user input passed as a variable to one function and return three other variables.  Those three variables get passed to another function to provide a result, and then the result is formatted to display.</p>

<p>Well, I'm attempting to use tkinter.  I've got an entry field which assigns the user input to a variable.  I've got a button linked to a function that is meant to start the ball rolling... and I do not know how to send that variable to the needed function(s), or how to take the returned variable and apply it.  In my specific example, I want to send the variable "notation" to the function "parse()", the output of "parse()" would be sent to "roll()", and the output of "roll()" sent to the variable "output" and then displayed.  All this would be started using the button with "command=calculate", with "calculate()" being a function that gets the entire ball rolling.</p>

<p>I am so very sorry... I am entirely self taught, and I am sure that I am not even using the right terminology for a lot of this.  I also understand that this example is not very pythonic - I will eventually put all of this into classes and change the functions to methods, but for now I just want to see it work.</p>

<p>Here is the code so far.  Fair warning, this is not the only problem I am having... I just want to stick to one question until I can get it worked out.</p>

<pre><code>#!/usr/bin/env python

from tkinter import *
from tkinter import ttk
import re
import random

# Parsing function from the original command line tool
# Turns dice notation format into useful variables

def parse(d):
    dice, dtype_mod = d.split('d')

    dnum = 1
    dtype = 6
    mod = 0

    if dtype_mod:
        if '-' in dtype_mod:
            dtype, mod = dtype_mod.split('-')
            mod = -1 * int(mod)
        elif '+' in dtype_mod:
            dtype, mod = dtype_mod.split('+')
            mod = int(mod)
        else:
            dtype = dtype_mod
    if not dtype: dtype = 6
    if not mod: mod = 0

    return (int(dice), int(dtype), int(mod))

# Rolling function from the original command line tool
# 'print()' will be changed into a variable, with the output 
# appended in the working version.

def roll(a, b):
    rolls = []
    t = 0

    for i in range(a):
        rolls.append(random.randint(1, b))
        t += int(rolls[i])
        print(('Roll number %d is %s, totaling %d') % (i + 1, rolls[i], t))
    return (int(t))

# Placeholder - the rest of the command line code will be used here later
# This code will be what starts everything rolling.
# For debugging, attempting to pass a variable, doing it wrong.

def calculate():
    output = "this is something different"
    return output

# Initialize

dice = Tk()
dice.title('Roll the Dice')
dice.geometry("800x600+20+20")

# Drawing the main frame

mainframe = ttk.Frame(dice, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

# Variables needed for program and widgets

random.seed()
notation = ""
output = """This is
an example of a lot of crap
that will be displayed here
if I ever get
this to work
and this is a really long line -Super Cali Fragil Istic Expi Ali Docious
"""

# Dice notation entry field, and it's label

notation_entry = ttk.Entry(mainframe, width=10, textvariable=notation)
notation_entry.grid(column=2, row=1)
ttk.Label(mainframe, text="Dice").grid(column=1, row=1)

# Section used for output
"""Huge laundry list of problems here:

1.  textvariable is not displaying anything here.  If I change it to text
    it seems to work, but from what I can tell, that will not update.
2.  I would love for it to have a static height, but height is not allowed here.
    Need to figure out a workaround.
3.  Also, have not figured out how to get the value returned by calculate()
    to show up in here when the button is pressed..."""

output_message = Message(mainframe, textvariable=output, width= 600)
output_message.grid(column=1, row=2, rowspan=3, columnspan=3)

# The 'make it go' button.
"""Can I pass the function a variable?"""

ttk.Button(mainframe, text="Roll!", command=calculate).grid(column=3, row=5)


# This is a bunch of stuff from the command line version of this program.
# Only here for reference

"""while True:
    notation = raw_input('Please input dice notation or q to quit: ')

    if notation == "q":
        raise SystemExit
    else:
         print(notation)

        numbers = parse(notation)
        (dice, dtype, mod) = numbers

        total = roll(dice, dtype)
        total += mod

        print('Your total is %d' % total)"""


dice.mainloop()
</code></pre>