# Global Variables and Tkinter

Page 1 of 1

## 4 Replies - 8831 Views - Last Post: 31 October 2011 - 12:23 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=253543&amp;s=1fea40464a66ea32236d01848e8655c1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Maximous

Reputation: 0
• Posts: 7
• Joined: 26-September 11

# Global Variables and Tkinter

Posted 30 October 2011 - 03:32 PM

I am working with tkinter to try to make a GUI for a Terminal Velocity Program. I have two problems: 1) I create a variable in one function and need to call it in another. I try to use a global variable but it doesn't seem to be working. 2) I am trying to learn how to make a label and entry disappear from the form if a certain radio button is clicked.

```# Imports all of tkinter and its variables/functions
from tkinter import*
# Lets square root function be used
from math import sqrt as sqrt
# imports messagebox and can be called as messagebox (instead of tkinter.messagebox)
import tkinter.messagebox as messagebox
gravityCount = 0
pressureCount = 0

# Creates a TK/GUI Screen called calculator
calculator = Tk()
# Titles it
calculator.title("Terminal Velocity Calculator")
# Sets width and height
calculator.geometry("400x400")

# Creates the function that happens when you click the button
def submit():
global numberGravity
global numberPressure
# Retrieves the number inputted in the Mass Entry
m = numberMass.get()
g = numberGravity.get()
p = numberPressure.get()
a = numberArea.get()
# Calculate Terminal Velocity
terminalVelocity = sqrt((4*m*g)/(p*a))
# Show an info messagebox displaying the Terminal Velocity
messagebox.showinfo("Terminal Velocity", terminalVelocity)

def isStandardGravity():
# imports count variable from outside this function
global gravityCount
# Get value from gravity buttons
gravityValue = gravityS.get()
# If no is selected, then display options to change Gravity
if gravityValue == "No" and gravityCount == 0:
# Stops repeated additoins of the entry and label
gravityCount = gravityCount + 1
gravity = Frame()
Label(gravity, text='Gravity = ').pack(side=LEFT)
# Variable contains a decimal
numberGravity = DoubleVar(None)
Entry(gravity, textvariable=numberGravity,).pack(side=LEFT)
elif gravityValue == "Yes" and gravityCount == 1:
####### INSERT CODE FOR MAKING THE ENTRY DISAPPEAR
elif gravityValue == "Yes" and gravityCount == 0:
numberGravity = DoubleVar(None)
numberGravity.set(9.8)

def isStandardPressure():
# imports count variable from outside this function
global pressureCount
# Get value from gravity buttons
pressureValue = pressureS.get()
# If no is selected, then display options to change Gravity
if pressureValue == "No" and pressureCount == 0:
# Stops repeated additoins of the entry and label
pressureCount = pressureCount + 1
pressure = Frame()
Label(pressure, text='Pressure = ').pack(side=LEFT)
# Variable contains a decimal
numberPressure = DoubleVar(None)
Entry(pressure, textvariable=numberPressure,).pack(side=LEFT)
elif pressureValue == "Yes" and pressureCount == 1:
####### INSERT CODE FOR MAKING THE ENTRY DISAPPEAR
elif pressureValue == "Yes" and pressureCount == 0:
numberPressure = DoubleVar(None)
numberPressure.set(1.22)

# Invisible frame that will hold the elemts you select it to
mass = Frame()
# Pack the frame and where you want it to be
# Creates Label and puts it in the Frame mass
Label(mass, text='Mass = ').pack(side=LEFT)
# Create the variable for the entry field and leave it blank
numberMass = DoubleVar(None)
# Creates the entry field
Entry(mass, textvariable=numberMass).pack(side=LEFT)

area = Frame()
area.pack()
Label(area, text="Surface Area = ").pack(side=LEFT)
numberArea = DoubleVar(None)
Entry(area, textvariable=numberArea).pack(side=LEFT)

# Buttons for Gravity
standardGravity = Frame()
Label(standardGravity, text="Standard Gravity").pack(side=TOP)
gravityS = StringVar()
gravityS.set(None)
# Create two radio buttons labeled Yes/No and check to see if standard gravity should be used
Radiobutton(standardGravity, text="Yes", value="Yes", variable = gravityS, command=isStandardGravity).pack()
Radiobutton(standardGravity, text="No", value="No", variable = gravityS, command=isStandardGravity).pack()

# Buttons for Air Pressure
standardPressure = Frame()
standardPressure.pack()
Label(standardPressure, text="Standard Pressure").pack(side=TOP)
pressureS = StringVar()
pressureS.set(None)
# Create two radio buttons labeled Yes/No and check to see if standard gravity should be used
Radiobutton(standardPressure, text="Yes", value="Yes", variable = pressureS, command=isStandardPressure).pack()
Radiobutton(standardPressure, text="No", value="No", variable = pressureS, command=isStandardPressure).pack()

# Create a frame for the button
button = Frame()
# Place the frame at the very bottom
# Create a button that calls the submit command when clicked
Button(button, text="Submit", command=submit).pack()

# The calculator window is constantly checked for inputs by user
calculator.mainloop()

```

Thanks for the help.

Is This A Good Question/Topic? 0

## Replies To: Global Variables and Tkinter

### #2 Motoma

Reputation: 452
• Posts: 797
• Joined: 08-June 10

## Re: Global Variables and Tkinter

Posted 31 October 2011 - 07:27 AM

1) Where is this not working?
2) You can use pack_forget() to remove a widget and pack() to place it back. If you want to preserve its position, however, you will need to enclose the widget in a frame and keep that frame in place (only pack_forget the widget, not the frame).

A quick example:
```from Tkinter import *

root = Tk()
root.title("Hide/Show")

f = Frame(root)
f.pack()

l = Label(f, text="Labels are for labeling.")
l.pack()

Button(root, text="Labels are stupid!", command=l.pack_forget).pack()
Button(root, text="Wait, I like labels!", command=l.pack).pack()

root.mainloop()

```

### #3 baavgai

• Dreaming Coder

Reputation: 6602
• Posts: 13,942
• Joined: 16-October 07

## Re: Global Variables and Tkinter

Posted 31 October 2011 - 08:05 AM

In submit() you say global numberGravity but you never have a variable of that name declared globally. Now, you did declare numberGravity inside isStandardGravity. Perhaps if you declared it global in there too...

Globals are simply not the way to go, though. You are creating all these objects. Perhaps if you made one of your own. You're just declaring too much stuff outside of a def or class. None of it should be declared that way.

None of you elements are really connected, so it's hard to follow what's going where and why.

Here's a quick example of passing data around, using your code:
```class Mass(Frame):
def __init__(self, master):
Frame.__init__(self, master)
Label(self, text='Mass = ').pack(side=LEFT)
self.numberMass = DoubleVar(None)
Entry(self, textvariable=self.numberMass).pack(side=LEFT)

class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.mass = Mass(self)
button = Frame(self)
Button(button, text="Submit", command=self.submit).pack()
self.pack()

def submit(self):
messagebox.showinfo("You entered", self.mass.numberMass.get())

calculator = Tk()
calculator.title("Terminal Velocity Calculator")
calculator.geometry("400x400")
app = Application(calculator)
calculator.mainloop()

```

Hope this helps.

### #4 Maximous

Reputation: 0
• Posts: 7
• Joined: 26-September 11

## Re: Global Variables and Tkinter

Posted 31 October 2011 - 12:16 PM

Motoma, on 31 October 2011 - 07:27 AM, said:

1) Where is this not working?

Lines 20 and 21 I am trying to get the variables from the functions 32 and 53. Sorry my code is messy, I am trying to teach my self.

### #5 Motoma

Reputation: 452
• Posts: 797
• Joined: 08-June 10

## Re: Global Variables and Tkinter

Posted 31 October 2011 - 12:23 PM

As baavgai said, numberGravity and numberPressure are not declared in the global scope, however, gravityCount and pressureCount are.

This post has been edited by Motoma: 31 October 2011 - 12:23 PM