2 Replies - 2498 Views - Last Post: 27 March 2012 - 06:33 AM Rate Topic: -----

#1 Computer-Wiz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-January 11

Python Tkinter: options in OptionMenu change width of window

Posted 26 March 2012 - 05:27 PM

Hello, i am having a little problem using the option menu in python. Its not a major problem but its just bothering me and i also want to know how to fix it. In my assignment i had to make a one rep max calculator using values inputted by the user(weight lifted and num of reps) i also have a option menu to display there rep weights in pounds and kilograms(2 options). The problem is that my default option is set to pounds so the windows(mainframe)of mine is normal. but when i change from pounds to kilograms the width of my window changes(i'm assuming it because kilograms is a larger word than pounds. So is there a way where the option selected wont make a differce to the window.my code is below:

from Tkinter import*

#Depending on the option chosen from option menu differet calculations are made for user input
#@param -none
#@return -none
def calculateButtonclick():
    if measurementVar.get() == "Pounds":
        poundsCalc()
    if measurementVar.get() == "Kilograms":
        kilogramsCalc()
        
#Takes user input and display one-rep-max and 45% to 95% weights in pounds unit. (Rounds answer to 0 place)
#@param -none
#@return -none
def poundsCalc():
#get weight lifted and number of reps from user   
    weightLift = weightLiftVar.get()
    numOfReps = numOfRepsVar.get()
#basic calculation to get one-rep-max weight in pounds
    highRepMax = weightLift / (1.0278 - (0.0278 * numOfReps))
    oneRepMaxVar.set(round(highRepMax ,0))
#weights for 45% to 95% in pounds    
    fortyfiveRepWeight = highRepMax  * 0.45
    fortyfiveVar.set(round(fortyfiveRepWeight, 0))
    fiftyRepWeight = highRepMax  * 0.50
    fiftyVar.set(round(fiftyRepWeight,0))
    fiftyfiveRepWeight = highRepMax * 0.55
    fiftyfiveVar.set(round(fiftyfiveRepWeight,0))
    sixtyRepWeight =highRepMax * 0.60
    sixtyVar.set(round(sixtyRepWeight,0))
    sixtyfiveRepWeight = highRepMax  * 0.65
    sixtyfiveVar.set(round(sixtyfiveRepWeight,0))
    seventyRepWeight =highRepMax  * 0.70
    seventyVar.set(round(seventyRepWeight, 0))
    seventyfiveRepWeight = highRepMax  * 0.75
    seventyfiveVar.set(round(seventyfiveRepWeight,0))
    eightyRepWeight = highRepMax  * 0.80
    eightyVar.set(round(eightyRepWeight,0))
    eightyfiveRepWeight = highRepMax  * 0.85
    eightyfiveVar.set(round(eightyfiveRepWeight,0))
    ninetyRepWeight = highRepMax  * 0.90
    ninetyVar.set(round(ninetyRepWeight,0))
    ninetyfiveRepWeight = highRepMax  * 0.95
    ninetyfiveVar.set(round(ninetyfiveRepWeight,0))
    
#Takes user input and display one-rep-max and 45% to 95% weights in kilograms unit. (Rounds answer to 0 place)
#@param -none
#@return -none    
def kilogramsCalc():
#get weight lifted and number of reps from user
    weightLift = weightLiftVar.get()
    numOfReps = numOfRepsVar.get()
#basic calculation to get one-rep-max weight in kilograms
    highRepMax = weightLift / (1.0278 - (0.0278 * numOfReps))*0.45359237
    oneRepMaxVar.set(round(highRepMax,0))
#weights for 45% to 95% in kilograms    
    fortyfiveRepWeight = (highRepMax * 0.45)*0.45359237
    fortyfiveVar.set(round(fortyfiveRepWeight, 0))
    fiftyRepWeight = (highRepMax * 0.5)*0.45359237
    fiftyVar.set(round(fiftyRepWeight,0))
    fiftyfiveRepWeight = (highRepMax * 0.55)*0.45359237
    fiftyfiveVar.set(round(fiftyfiveRepWeight,0))
    sixtyRepWeight = (highRepMax * 0.6)*0.45359237
    sixtyVar.set(round(sixtyRepWeight,0))
    sixtyfiveRepWeight = (highRepMax * 0.65)*0.45359237
    sixtyfiveVar.set(round(sixtyfiveRepWeight,0))
    seventyRepWeight = (highRepMax * 0.7)*0.45359237
    seventyVar.set(round(seventyRepWeight, 0))
    seventyfiveRepWeight = (highRepMax* 0.75)*0.45359237
    seventyfiveVar.set(round(seventyfiveRepWeight,0))
    eightyRepWeight = (highRepMax * 0.8)*0.45359237
    eightyVar.set(round(eightyRepWeight,0))
    eightyfiveRepWeight = (highRepMax * 0.85)*0.45359237
    eightyfiveVar.set(round(eightyfiveRepWeight,0))
    ninetyRepWeight = (highRepMax * 0.9)*0.45359237
    ninetyVar.set(round(ninetyRepWeight,0))
    ninetyfiveRepWeight = (highRepMax* 0.95)*0.45359237
    ninetyfiveVar.set(round(ninetyfiveRepWeight,0))

#If button is clicked all values from every entrybox is cleared for new input. Also set Pounds as default unit.
#@param -none
#@return -none    
def resetButton():
    weightLiftVar.set("")
    numOfRepsVar.set("")
    measurementVar.set ("Pounds")
    fortyfiveVar.set("")
    fiftyVar.set("")
    fiftyfiveVar.set("")
    sixtyVar.set("")
    sixtyfiveVar.set("")
    seventyVar.set("")
    seventyfiveVar.set("")
    eightyVar.set("")
    eightyfiveVar.set("")
    ninetyVar.set("")
    ninetyfiveVar.set("")
    oneRepMaxVar.set("")

#Creating the Mainframe(holding frame)
root = Tk()
root.title("Rep Max Calculator")
mainframe = Frame(root)

######
#MAIN#
######

#IntVar()
#set ("") so 0's dont show when entry is empty
weightLiftVar = IntVar()
weightLiftVar.set("")
numOfRepsVar = IntVar()
numOfRepsVar.set("")
fortyfiveVar = IntVar()
fortyfiveVar.set("")
fiftyVar = IntVar()
fiftyVar.set("")
fiftyfiveVar = IntVar()
fiftyfiveVar.set("")
sixtyVar = IntVar()
sixtyVar.set("")
sixtyfiveVar = IntVar()
sixtyfiveVar.set("")
seventyVar = IntVar()
seventyVar.set("")
seventyfiveVar = IntVar()
seventyfiveVar.set("")
eightyVar = IntVar()
eightyVar.set("")
eightyfiveVar = IntVar()
eightyfiveVar.set("")
ninetyVar = IntVar()
ninetyVar.set("")
ninetyfiveVar = IntVar()
ninetyfiveVar.set("")
oneRepMaxVar = IntVar()
oneRepMaxVar.set("")

#StringVar()
measurementVar = StringVar()

#All Widgets created in mainframe
titleLabel = Label (mainframe, text = "Rep Max Calculator", font = ("Arial", 16, "bold"),justify = CENTER)
weightLiftLabel = Label(mainframe, text = "Weigth Lifted:",  font = ("Arial", 12))
weightLiftEntry = Entry(mainframe, width = 10, bd =3,textvariable =weightLiftVar)
numOfRepsLabel = Label(mainframe, text = "Number Of Reps:",font = ("Arial", 12))
numOfRepsEntry = Entry(mainframe, width = 10, bd =3,textvariable = numOfRepsVar)
fortyfiveLabel = Label(mainframe, text = "45%:", font = ("Arial", 12), justify = RIGHT, anchor = E) 
fiftyLabel= Label(mainframe, text = "50%:", font = ("Arial", 12), justify = RIGHT,  anchor = E)
fiftyfiveLabel= Label(mainframe, text = "55%:", font = ("Arial", 12), justify = RIGHT, anchor = E)
sixtyLabel= Label(mainframe, text = "60%:", font = ("Arial", 12), justify = RIGHT, anchor = E)
sixtyfiveLabel= Label(mainframe, text = "65%:", font = ("Arial", 12), justify = RIGHT, anchor = E)
seventyLabel= Label(mainframe, text = "70%:", font = ("Arial", 12), justify = RIGHT, anchor = E)
seventyfiveLabel= Label(mainframe, text = "75%:", font = ("Arial", 12))
eightyLabel= Label(mainframe, text = "80%:", font = ("Arial", 12))
eightyfiveLabel= Label(mainframe, text = "85%:", font = ("Arial", 12))
ninetyLabel= Label(mainframe, text = "90%:", font = ("Arial", 12))
ninetyfiveLabel= Label(mainframe, text = "95%:", font = ("Arial", 12))
oneRepMaxLabel = Label(mainframe, text = "1 Rep Max:", font = ("Arial", 12))
#every entrybox is attached to its own IntVar for changes
fortyfiveEntry= Entry(mainframe, width = 10, bd =3,textvariable = fortyfiveVar)
fiftyEntry= Entry(mainframe, width = 10, bd =3,textvariable = fiftyVar)
fiftyfiveEntry= Entry(mainframe, width = 10, bd =3,textvariable = fiftyfiveVar)
sixtyEntry= Entry(mainframe, width = 10, bd =3,textvariable = sixtyVar)
sixtyfiveEntry= Entry(mainframe, width = 10, bd =3,textvariable = sixtyfiveVar)
seventyEntry= Entry(mainframe, width = 10, bd =3,textvariable = seventyVar)
seventyfiveEntry= Entry(mainframe, width = 10, bd =3,textvariable = seventyfiveVar)
eightyEntry= Entry(mainframe, width = 10, bd =3,textvariable = eightyVar)
eightyfiveEntry= Entry(mainframe, width = 10, bd =3,textvariable = eightyfiveVar)
ninetyEntry= Entry(mainframe, width = 10, bd =3,textvariable = ninetyVar)
ninetyfiveEntry= Entry(mainframe, width = 10, bd =3,textvariable = ninetyfiveVar)
oneRepMaxEntry = Entry(mainframe, width = 10, bd =3,textvariable = oneRepMaxVar)
measurementLabel = Label(mainframe, text = "Measurement:", font = ("Arial", 12, "bold"))
measurementOption = OptionMenu (mainframe, measurementVar, "Pounds", "Kilograms")
measurementVar.set("Pounds")
calculateButton = Button (mainframe, text = "Calculate", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command=calculateButtonclick)
resetButton = Button (mainframe, text = "Reset", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "green", overrelief = GROOVE,activebackground = "blue", activeforeground="red", command=resetButton)

#Widgets grided to mainframe
mainframe.grid()
titleLabel.grid(row = 0, column = 0, columnspan = 7, pady = 20)
weightLiftLabel.grid(row = 1, column = 1, sticky = N+E)
weightLiftEntry.grid(row = 1, column = 2)
numOfRepsLabel.grid(row = 2, column = 1, ipady = 10, sticky = N, rowspan = 2)
numOfRepsEntry.grid(row = 2, column = 2)
oneRepMaxLabel.grid(row = 5, column = 1, sticky = E)
oneRepMaxEntry.grid(row = 5, column = 2)
ninetyfiveLabel.grid(row = 6, column = 1, sticky = E)
ninetyfiveEntry.grid(row = 6, column = 2)
ninetyLabel.grid(row = 7, column = 1, sticky = E)
ninetyEntry.grid(row = 7, column = 2)
eightyfiveLabel.grid(row = 8, column = 1, sticky = E)
eightyfiveEntry.grid(row = 8, column = 2)
eightyLabel.grid(row = 9, column = 1, sticky = E)
eightyEntry.grid(row = 9, column = 2)
seventyfiveLabel.grid(row = 10, column = 1, sticky = E)
seventyfiveEntry.grid(row = 10, column = 2)
seventyLabel.grid(row = 5, column = 5, sticky = E)
seventyEntry.grid(row = 5, column = 6, sticky = W)
sixtyfiveLabel.grid(row = 6, column = 5, sticky = E)
sixtyfiveEntry.grid(row = 6, column = 6, sticky = W)
sixtyLabel.grid(row = 7, column = 5, sticky = E)
sixtyEntry.grid(row = 7, column = 6,  sticky = W)
fiftyfiveLabel.grid(row = 8, column = 5, sticky = E)
fiftyfiveEntry.grid(row = 8, column = 6,  sticky = W)
fiftyLabel.grid(row = 9, column = 5, sticky = E)
fiftyEntry.grid(row = 9, column = 6,  sticky = W)
fortyfiveLabel.grid(row = 10, column = 5, sticky = E)
fortyfiveEntry.grid(row = 10, column = 6, sticky = W)
measurementLabel.grid(row = 1, column = 5, sticky = W+E)
measurementOption.grid(row = 1, column = 6, padx = 10)
calculateButton.grid(row = 2, column = 5, sticky = W, padx = 20 )
resetButton.grid(row = 2, column = 6, sticky = W, padx = 10)

root.mainloop()




Is This A Good Question/Topic? 0
  • +

Replies To: Python Tkinter: options in OptionMenu change width of window

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python Tkinter: options in OptionMenu change width of window

Posted 26 March 2012 - 07:53 PM

right after you make your option menu, add this line
measurementOption.config(width=len("kilograms"))

Was This Post Helpful? 1
  • +
  • -

#3 Computer-Wiz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-January 11

Re: Python Tkinter: options in OptionMenu change width of window

Posted 27 March 2012 - 06:33 AM

Thanks man it worked
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1