4 Replies - 2550 Views - Last Post: 31 October 2011 - 12:23 PM Rate Topic: -----

#1 Maximous  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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()
        gravity.pack(pady=10)
        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()
        pressure.pack(pady=10)
        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
mass.pack(pady=10)
# 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()
standardGravity.pack(pady=10)
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
button.pack(pady=10, side=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  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • 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()


Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • 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)
		self.pack(pady=10)
		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.pack(pady=10, side=BOTTOM)
		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.
Was This Post Helpful? 0
  • +
  • -

#4 Maximous  Icon User is offline

  • New D.I.C Head

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

Re: Global Variables and Tkinter

Posted 31 October 2011 - 12:16 PM

View PostMotoma, 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.
Was This Post Helpful? 0
  • +
  • -

#5 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1